Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为UITableView中的每个单元格指定一个数字_Ios_Uitableview - Fatal编程技术网

Ios 为UITableView中的每个单元格指定一个数字

Ios 为UITableView中的每个单元格指定一个数字,ios,uitableview,Ios,Uitableview,我写了一个代码,给TableView中的每个单元格一个从8开始的数字,当它达到12时,我在apphours中称之为计数器 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeu

我写了一个代码,给TableView中的每个单元格一个从8开始的数字,当它达到12时,我在apphours中称之为计数器

- (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;}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
而(小时<15)
{
如果(小时==12){
小时=1;
}否则{
NSString*str=[NSString stringWithFormat:@“%d”,小时];
cell.textlab.text=[courses objectAtIndex:indexath.row];
cell.detailTextLabel.text=str;
NSLog(@“%@”,str);
小时++;
返回单元;
}
}
返回单元格;}
当我使用模拟器运行应用程序时,这很好,但当我滚动时,它会不断更改第一个和第二个单元格中的数字,我希望即使滚动表格,数字也不会更改

我怎么能做到?
提前感谢。

看起来
hour
是在方法之外定义的,并且没有链接到单元格的
indepath
。如果您希望确保显示的字符串对于单元格保持不变,那么您应该能够仅从
indexPath
确定地计算它,因此即使表格无序加载单元格,它们将保留正确的文本。

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

oh snap,不管我说了什么,这就是我没有正确阅读代码所得到的。在UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]之后添加此代码;///如果(cell==nil){cell=[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]autorelease];}哦,snap,不管我说什么,xd这就是我没有正确阅读代码的结果。在UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forindexath:indexPath]之后添加此代码;///如果(cell==nil){cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:MyIdentifier]autorelease];}我怎么做?很抱歉,我是一个初学者。这真的取决于什么定义了你的单元格应该如何显示数字?单元格1是否应显示
1
,#2
2
等等?如果是,则将hour设置为
indexpath.row
。你想展示什么样的图案?我怎么做?很抱歉,我是一个初学者。这真的取决于什么定义了你的单元格应该如何显示数字?单元格1是否应显示
1
,#2
2
等等?如果是,则将hour设置为
indexpath.row
。要显示的图案是什么?