uitableview - 数据和if(!cell)

时间:2012-02-20 17:38:41

标签: iphone xcode uitableview

我对UITableView有一个很大的问题,我想在单元格中使用一个标签,所以我用这个方法来做它

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ItemCell";

// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {

}

if (indexPath.section == 0) {

    elemento = [array objectAtIndex:indexPath.row];

    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
    labelTitle.text = [elemento objectForKey:@"Titolo"];
    labelTitle.backgroundColor = [UIColor clearColor];
    labelTitle.textColor = [UIColor whiteColor];
    [cell addSubview:labelTitle];

} else {

    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
    labelTitle.text = @"Read More";
    labelTitle.backgroundColor = [UIColor clearColor];
    labelTitle.textColor = [UIColor whiteColor];
    [cell addSubview:labelTitle];

}


return cell;

}

通过这种方式我可以看到我桌上的所有数据,但标签是重叠的,而不是我尝试使用这种方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ItemCell";

// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    if (indexPath.section == 0) {

        elemento = [array objectAtIndex:indexPath.row];

        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
        labelTitle.text = [elemento objectForKey:@"Titolo"];
        labelTitle.backgroundColor = [UIColor clearColor];
        labelTitle.textColor = [UIColor whiteColor];
        [cell addSubview:labelTitle];

    } else {

        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
        labelTitle.text = @"Read More";
        labelTitle.backgroundColor = [UIColor clearColor];
        labelTitle.textColor = [UIColor whiteColor];
        [cell addSubview:labelTitle];

    }

}
else {

}

return cell;

}

标签没问题但是在这种情况下我只能在我的桌子上看到5个数据,这5个数据重复了一段时间......

例如,如果在我桌子上的第一种情况下我可以看到:1,2,3,4,5,6,7,8,9,10,... 在第二种情况下,我看到:1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,......

问题出在哪里?

2 个答案:

答案 0 :(得分:1)

添加此代码

  for (UIView *view in [cell.contentView subviews]) 
    {
        [view removeFromSuperview];
    }

之前

if(indexPath.section == 0){

elemento = [array objectAtIndex:indexPath.row];

UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:@"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];

目前,您向单元格添加了一个标签,下次重复使用该单元格时,标签仍然存在,并在其上添加标签。

答案 1 :(得分:0)

您发布的代码指定UITableViewCellStyleSubtitle作为单元格样式,这意味着每个单元格都会在其相应的属性textLabeldetailTextLabel中提供文本标签和详细文本标签。因此,您没有理由分配UILabel的其他实例。相反,只需填充现有标签的text属性即可。例如,您可以像这样重写您的实现:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellID = @"ItemCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];

    }

    cell.textLabel.text = (indexPath.section == 0 ?
                           [array objectAtIndex:indexPath.row] :
                           @"ReadMore");    

    return cell;
}