从自定义的UITableViewCell获取值

我有一个自定义的UITableViewCell有一个UISegmentedControl对象。 我的UITableView(问卷forms)有6个这个自定义单元格(6个​​分段控件)。 我无法获取段控件的selectedSegmentIndex 。 我的猜测是,表格生成后,单元格被释放。 我正在使用下面的代码获取值:

 MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]]; int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex; 

int segmentIndex始终给出相同的值0而不pipe选定的索引是什么。

您必须使用正确的方法初始化您要使用的部分和行的索引path,否则rowsection属性将无法正确设置,并且每次都会得到相同的单元格:
+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

代码如下所示:

 // getting the cell at third row of first section NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0]; MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:ip]; int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex; 

你也可以参考: NSIndexPath UIKit Additions Reference获取更多信息。