如何避免标签文本在iPhone sdk的uitableview中重新加载?

问题描述:

我的网络服务中有一些产品。我从中获取产品名称,并使用标签文本将其显示在我的tableview中。我明白了。现在我的问题是,当我在我的tableview中选择某个产品时,即使我的didselectrowatindexpath为空,我的标签也会重新加载并覆盖现有的标签文本。我用这个,如何避免标签文本在iPhone sdk的uitableview中重新加载?

- (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 
{ 

} 
+0

确定...请你给这个问题的建议? – user579911 2012-04-05 10:50:18

+0

你在哪里分配细胞?你在使用静态单元吗? – Ilanchezhian 2012-04-05 10:50:41

+1

仅在cellforrowatindexpath内分配 – user579911 2012-04-05 10:52:25

使用以下cellForRowAtIndexPath方法。

- (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]; 
} 
+0

感谢您的精彩和直接的回应......它的工作。 – user579911 2012-04-05 11:08:17

在UILabel代码上面添加下面的代码。这可能对你有帮助。

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

非常感谢它的工作。 – user579911 2012-04-05 11:04:42