【问题标题】:get value from custom UITableViewCell从自定义 UITableViewCell 中获取值
【发布时间】:2011-07-26 00:34:09
【问题描述】:

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

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

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

无论选择的索引是什么,int segmentIndex 始终给出相同的 0 值。

【问题讨论】:

    标签: objective-c ios cocoa-touch uitableview


    【解决方案1】:

    您必须使用正确的方法来初始化您要使用的节和行的索引路径,否则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 了解更多信息。

    【讨论】:

    • 我一开始使用indexPathForRow:inSection,但由于某种原因它不起作用。
    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多