ios - 为什么我的自定义 UITableViewCell 永远不会从原型(prototype)发生变化?

标签 ios ios6

我有一个扩展 UITableViewCell 的自定义类。它有两个标签和一个 UISegmentedControl。

这是我配置的 cellForRowAtIndexPath()。当我在调试器中检查“单元格”时,它包含我提供的所有数据。但不知何故,这些数据永远不会被应用。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    CustomGameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[CustomGameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    MyData *my_data = [rows objectAtIndex:indexPath.row];

    UILabel *my_date = [[UILabel alloc] init];
    my_date.text = my_data.myDate;
    [cell setMyDateLabel:my_date];

    UILabel *my_question = [[UILabel alloc] init];
    my_question.text = my.question;
    [cell setMyQuestionLabel:my_question];


    UISegmentedControl *my_choices = [[UISegmentedControl alloc]
                                        initWithItems:[NSArray arrayWithObjects:my.firstChoice, my.secondChoice, nil]];
    [my_choices setSelectedSegmentIndex:my.choice];
    [cell setMyChoiceSegments:my_choices];

    return cell
}

我想要显示的数据当前位于我在 viewDidLoad() 中创建的数组中,cellForRowAtIndexPath() 可以通过“rows”变量访问该数组。

当我在模拟器中运行代码时,我在表中得到三行,代表我在 viewDidLoad() 中创建的数组中的三个元素。但是,这些行的内容看起来与我在 Storyboard中定义的内容完全相同。

我错过了什么?

最佳答案

  1. 您在哪里定义单元格的布局?在 NIB 中?在你的 Storyboard里?以编程方式在 CustomGameCellinitWithStyle 中?根据您使用的方法,实现细节略有不同,但您肯定需要在 Storyboard 中定义 NIB 或原型(prototype)单元,或者以编程方式创建控件,设置其框架,执行 addSubview 以便它们'重新包含在单元格中等。

  2. 您的代码正在添加新的 UILabel 对象,而不是将它们作为 subview 添加到任何内容中,无论您是否使用出列单元格,都会执行此操作,等等。因此,有很多这里出现问题。要查看如何正确使用自定义单元的示例,请参阅 Customizing Cells TableView 编程指南中。但是,就像我说的,细节根据您设计子类 UITableViewCell 布局的方式而有所不同,所以我犹豫是否提出任何建议代码,直到您指定如何设计用户界面。

关于ios - 为什么我的自定义 UITableViewCell 永远不会从原型(prototype)发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524525/

相关文章:

ios - 链接到应用商店中另一个应用的正确方法是什么?

ios - 手动为字典赋值和使用 updateValue 函数有什么区别?

ios - 为什么我的 UIAlertController 直到我第二次按下它的选项卡时才出现?

ios - 如何选择 CollectionViewCell 中的 TableViewCell

ios - Facebook sdk 3.5.1 邀请好友请求在 ios 中进行双重选择

ios - 自定义 map 路径线

ios - 动画转换 UILabel 的一部分

iphone - 我的联系人未添加到 ios 7 的地址簿中

ios - 动态属性文本

ios - 检查特定对象的数组