user579911
user579911

Reputation: 63

How to avoid label text reloading in uitableview in iPhone sdk?

Am having some products in my web service. I am getting the product name from that and displaying it in my tableview using label text. And i got that. Now my problem is, when am selecting some product in my tableview, my label re-loads and overwriting with my existing label text, even when my didselectrowatindexpath is empty. I used this,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
    bookName = [[UILabel alloc]initWithFrame:CGRectMake(100, 3, 180, 25)];
    NSString *text = [NSString stringWithFormat:@"%@",[[stories objectAtIndex:indexPath.row]    objectForKey: @"product_name"]];
    bookName.text = text;
    bookName.textAlignment = UITextAlignmentCenter;
    bookName.lineBreakMode = UILineBreakModeWordWrap;
    [bookName setTextColor:[UIColor blackColor]];

    CGSize expectedLabelSize = [text sizeWithFont:bookName.font constrainedToSize:bookName.frame.size lineBreakMode:UILineBreakModeWordWrap];
    CGRect newFrame = bookName.frame;
    newFrame.size.height = expectedLabelSize.height;
    bookName.frame = newFrame;
    bookName.numberOfLines = 0;
    [bookName sizeToFit];
    [cell.contentView addSubview:bookName];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

}

Upvotes: 0

Views: 868

Answers (2)

Akshay Aher
Akshay Aher

Reputation: 2525

Add following code just above UILabel code.This might help you.

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

Upvotes: 3

Ilanchezhian
Ilanchezhian

Reputation: 17478

Use the following cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }

    UILabel *bookName = (UILabel*)[cell.contentView viewWithTag:1001];
    if( !bookName )
    {
        bookName = [[UILabel alloc]initWithFrame:CGRectMake(100, 3, 180, 25)];
        [cell.contentView addSubview:bookName];
        bookName.tag = 1001;
        bookName.numberOfLines = 0;
        bookName.textAlignment = UITextAlignmentCenter;
        bookName.lineBreakMode = UILineBreakModeWordWrap;
        [bookName setTextColor:[UIColor blackColor]];
    }

    NSString *text = [NSString stringWithFormat:@"%@",[[stories objectAtIndex:indexPath.row]    objectForKey: @"product_name"]];
    bookName.text = text;

    CGSize expectedLabelSize = [text sizeWithFont:bookName.font constrainedToSize:bookName.frame.size lineBreakMode:UILineBreakModeWordWrap];
    CGRect newFrame = bookName.frame;
    newFrame.size.height = expectedLabelSize.height;
    bookName.frame = newFrame;
    [bookName sizeToFit];
}

Upvotes: 2

Related Questions