UITableViewcell不能重复使用

时间:2013-07-20 11:33:16

标签: ios uitableview

下面显示的是我用来创建单元格的代码。该单元格未被重用。每次cell==nil成为现实。

我正在xib中正确设置标识符。请帮帮我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SRCourseListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) {

        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"SRCourseListCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];

    }

    return cell;
}

3 个答案:

答案 0 :(得分:2)

在您的SRCourseListCell中,添加

- (NSString *) reuseIdentifier {
  return @"cell";
}

或(因为你使用笔尖可能是更好的解决方案),设置identifier to "cell" in the inspector

答案 1 :(得分:1)

在您的“SRCourseListCell.xib”中,转到 属性检查器 并为 标识符

the Attributes Inspector

将以下修改过的代码替换为令人兴奋的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SRCourseListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SRCourseListCell"];
    if (cell == nil) 
    {
        NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"SRCourseListCell" owner:self options:nil];
        cell=[nib objectAtIndex:0];
    }
    return cell;
}

为自定义单元格调用xib时,请确保xib的标识符必须与您使用的相同:

[tableView dequeueReusableCellWithIdentifier:@"SRCourseListCell"]

答案 2 :(得分:-1)

使用以下代码进行初始化:

    YourTableViewCell *cel = [[YourTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];