uitableview - data and if (!cell)



我在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,。。。

问题出在哪里?

添加此代码

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

之前

如果(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];

当前,您将标签添加到单元格中,下次重复使用该单元格时。。标签仍然存在,您可以在其上添加一个标签。

您发布的代码将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;
}

最新更新