开发者

UItableView multiplle data printed

I am creating grouped table in my application. It works fine. But when I scroll the table upward and backward reprinted text on previous cell. It look like horrible.

This is my Code

- (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]开发者_Go百科 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;  
}


Update

My original answer was not the correct way to reuse UITableViewCell's. You should never use "CellID%d%d"(indexPath.row & indexPath.column) as cell identifier, that defeats the very purpose of cell reuse. It will create separate UITableViewCell for every row in the table, and every one of those cells stays in memory. Imagine what will happen if you have a tableView with around 1000 rows.

The real answer to OP's question is - make sure to reset a UITableViewCell returned by dequeueReusableCellWithIdentifier because it might return a cell that is already used and its UI populated. For example, if you didn't reset the texts in UILabel, it might contain strings that should have been shown in a different row of your tableView.

My original Answer (Don't use this)

Hiren, just try,

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];  
}

instead of

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

Please read this..you will understand how to give cell identifiers..


When you create the UITableView object, Set the rowHeight for each cell.

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

Let me know if you still struggle to get the desire result.

If your cell's height is not constant then implement heightForRowAtIndexPath method,

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

And return different-2 height for each cell.


try adding any color because clearcolor is making label transparent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜