Ios 为什么我的自定义UITableViewCell从未改变过原型?

Ios 为什么我的自定义UITableViewCell从未改变过原型?,ios,ios6,Ios,Ios6,我有一个扩展UITableViewCell的自定义类。它有两个标签和一个UISegmentedControl 这是我配置的CellForRowatineXpath。当我在调试器中检查单元格时,它拥有我提供的所有数据。但不知何故,这些数据从未得到应用 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *

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

这是我配置的CellForRowatineXpath。当我在调试器中检查单元格时,它拥有我提供的所有数据。但不知何故,这些数据从未得到应用

-(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中创建的数组中的三个元素。但是,这些行的内容看起来与我在故事板中定义的内容完全相同


我遗漏了什么?

您必须在单元格的“单元格内容”视图中添加了标签和分段控件,如果没有,请这样做

-(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];

    cell.myDateLabel.text = my_data.myDate;

    cell.myQuestionLabel.text = my.question;

    [cell.myChoiceSegments setSelectedSegmentIndex:my.choice];

    [cell autorelease];
    return cell
}

还可以使用自动释放进行内存管理。

必须在单元格的单元格内容视图中添加标签和分段控件,如果没有,请执行此操作

-(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];

    cell.myDateLabel.text = my_data.myDate;

    cell.myQuestionLabel.text = my.question;

    [cell.myChoiceSegments setSelectedSegmentIndex:my.choice];

    [cell autorelease];
    return cell
}
还可以使用自动释放进行内存管理

您在哪里定义单元格的布局?在笔尖里?在你的故事板里?在您的initWithStyle CustomGameCell中以编程方式?实现细节根据您使用的方法略有不同,但您肯定需要在故事板中定义NIB或原型单元,或者以编程方式创建控件、设置其框架、执行addSubview以使其包含在单元中,等等

您的代码正在添加新的UILabel对象,而不是将它们作为子视图添加到任何内容中,无论您是否使用出列单元格,都要这样做,等等。因此,这里有许多问题。要查看如何正确使用自定义单元格的示例,请参见《表视图编程指南》中的。但是,正如我所说的,细节会因您设计子类UITableViewCell布局的方式而有所不同,因此在您指定如何设计用户界面之前,我不会提出任何代码

您在哪里定义单元格的布局?在笔尖里?在你的故事板里?在您的initWithStyle CustomGameCell中以编程方式?实现细节根据您使用的方法略有不同,但您肯定需要在故事板中定义NIB或原型单元,或者以编程方式创建控件、设置其框架、执行addSubview以使其包含在单元中,等等

您的代码正在添加新的UILabel对象,而不是将它们作为子视图添加到任何内容中,无论您是否使用出列单元格,都要这样做,等等。因此,这里有许多问题。要查看如何正确使用自定义单元格的示例,请参见《表视图编程指南》中的。但是,正如我所说的,细节会因您设计子类UITableViewCell布局的方式而有所不同,因此在您指定如何设计用户界面之前,我不会提出任何代码


我试试看。但是我需要用ARC自动释放吗?@DarrellBrogdon你不需要。Shekhar显然认为你没有使用ARC。我不知道为什么现在有人不使用ARC!我试试看。但是我需要用ARC自动释放吗?@DarrellBrogdon你不需要。Shekhar显然认为你没有使用ARC。我不知道为什么现在有人不使用ARC!