0

我写了一个代码,给我的 TableView 中的每个单元格一个从 8 开始的数字,当它达到 12 时,我在应用程序小时内调用它的计数器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

while (hour < 15)
{
    if (hour == 12) {
        hour=1;
    } else {
        NSString *str = [NSString stringWithFormat:@"%d",hour];
        cell.textLabel.text = [courses objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = str;
        NSLog(@"%@",str);
        hour++;
        return cell;

    }
}

return cell;}

当我用模拟器运行应用程序时效果很好,但是当我滚动它时,它会不断更改第一个和第二个单元格中的数字,我希望即使滚动表格也不会改变数字。

我该怎么做?提前致谢。

4

1 回答 1

4

它看起来像是hour在方法之外定义的,并且没有链接到单元格的indexPath. 如果您想确保单元格的显示字符串保持不变,您应该能够确定地indexPath单独计算它,因此即使表格无序加载单元格,它们也会保持正确的文本。

于 2013-03-27T04:11:59.650 回答