OGeek|极客世界-中国程序员成长平台

标题: ios - 为什么我的自定义 UITableViewCell 永远不会从原型(prototype)中改变? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:50
标题: ios - 为什么我的自定义 UITableViewCell 永远不会从原型(prototype)中改变?

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

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

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 中定义的完全一样。

我错过了什么?



Best Answer-推荐答案


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

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

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4