我写了一段代码,为我的 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;}
当我使用模拟器运行应用程序时效果很好,但当我滚动时它会不断更改第一个和第二个单元格中的数字,我希望即使我滚动表格也不会更改数字。
我该怎么做? 提前致谢。
最佳答案
看起来 hour
是在方法之外定义的,并且没有链接到单元格的 indexPath
。如果你想确保显示的字符串对于一个单元格保持不变,你应该能够单独从 indexPath
确定地计算它,因此即使表格加载单元格顺序不正确,它们也会保持正确的文本。
关于ios - 为 UITableView 中的每个单元格提供一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651733/