本文介绍了从自定义UITableViewCell获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom UITableViewCell that has a UISegmentedControl object. My UITableView (questionnaire form) has 6 of this custom cell (6 segmented controls). I am having trouble getting the selectedSegmentIndex of the segment controls. My guess is that the cells are being released after the table is generated. I am getting the values using the following code:

MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]];

int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;

int segmentIndex 无论选择的索引是什么,值 0

the int segmentIndex is always giving the same value of 0 no matter what the selected index is.

推荐答案

您必须使用正确的方法初始化您要使用的部分和行的索引路径,否则 row 属性将不会正确设置,并且每次都将获得相同的单元格:

+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

You must use the right method to initialize your index path for the section and row you want use, otherwise row and section properties won't be correctly set, and you will get the same cell each time :
+(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;

您也可以咨询:了解更多信息。

You could also consult : NSIndexPath UIKit Additions Reference for more information.

这篇关于从自定义UITableViewCell获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:24