UItableView打印多个数据

时间:2011-04-22 10:38:42

标签: ios cocoa-touch uitableview

我在我的应用程序中创建分组表。 它工作正常。但是当我向上和向后滚动表格时,在前一个单元格上重新打印了文本。它看起来很糟糕。

这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"cellID"];

    if (!cell)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease];  
    }
 /*
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    */

    if(indexPath.section == 0)
    {
        cell.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryNone;

        float rowHeight = [DetailViewController calculateHeightOfTextFromWidth:[dict objectForKey:@"golf_name"] :[UIFont boldSystemFontOfSize:18] :290 :UILineBreakModeTailTruncation];

        UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 290, rowHeight)];
        categorName.font = [UIFont boldSystemFontOfSize:20];
        categorName.numberOfLines = 5;
        categorName.backgroundColor = [UIColor clearColor];
        categorName.textColor = [UIColor whiteColor];
        categorName.text = [dict objectForKey:@"golf_name"];
        [cell addSubview:categorName];
        [categorName release];
    }
    if(indexPath.section == 1)
    {
        cell.backgroundColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryNone;

        UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 70, 20)];
        categorName.font = [UIFont boldSystemFontOfSize:15];
        categorName.numberOfLines = 5;
        categorName.backgroundColor = [UIColor clearColor];
        categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
        categorName.text = @"Phone";
        [cell addSubview:categorName];
        [categorName release];

        UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 290, 20)];
        phone.font = [UIFont boldSystemFontOfSize:15];
        phone.numberOfLines = 5;
        phone.backgroundColor = [UIColor clearColor];
        //categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
        phone.text = [dict objectForKey:@"golf_phone"];
        [cell addSubview:phone];
        [phone release];
    }
    if(indexPath.section == 2)
    {
        cell.backgroundColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryNone;

        UILabel *categorName = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 90, 20)];
        categorName.font = [UIFont boldSystemFontOfSize:15];
        categorName.numberOfLines = 5;
        categorName.backgroundColor = [UIColor clearColor];
        categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
        categorName.text = @"Home page";
        [cell addSubview:categorName];
        [categorName release];

        UILabel *phone = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 230, 20)];
        phone.font = [UIFont boldSystemFontOfSize:15];
        phone.numberOfLines = 5;
        phone.backgroundColor = [UIColor clearColor];
        //categorName.textColor = [UIColor colorWithRed:145.0f/255.0f green:162.0f/255.0f blue:184.0f/255.0f alpha:1];
        phone.text = [dict objectForKey:@"golf_url"];
        [cell addSubview:phone];
        [phone release];
    }  
.......  
.....  
return cell;  
}

3 个答案:

答案 0 :(得分:1)

更新

我原来的答案不是重用UITableViewCell的正确方法。您永远不应该使用“CellID%d%d”(indexPath.row& indexPath.column)作为单元标识符,这会破坏单元重用的目的。它将为表中的每一行创建单独的UITableViewCell,并且每个单元都保留在内存中。想象一下,如果你有一个大约1000行的tableView会发生什么。

OP问题的真正答案是 - 确保重置dequeueReusableCellWithIdentifier返回的UITableViewCell,因为它可能会返回已使用的单元格并填充其UI。例如,如果您没有重置UILabel中的文本,它可能包含应该在tableView的不同行中显示的字符串。

我原来的答案(不要用这个)

Hiren,试试吧,

UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:@"CellID%d%d",indexPath.section,indexPath.row]] autorelease];  
}

而不是

UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"cellID"];
if (!cell)
{
   cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cellID"] autorelease];  
}

请阅读this ..您将了解如何提供单元格标识符..

答案 1 :(得分:0)

创建UITableView对象时,请为每个单元格设置rowHeight

myTableView.rowHeight = 50; //Depend on your requirement,

如果您仍然难以获得渴望的结果,请告诉我。

如果您的单元格高度不恒定,请执行heightForRowAtIndexPath方法,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

每个细胞返回不同的高度。

答案 2 :(得分:0)

尝试添加任何颜色,因为clearcolor使标签透明。