2012-04-05 50 views
0

我的網絡服務中有一些產品。我從中獲取產品名稱,並使用標籤文本將其顯示在我的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

回答

2

使用以下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

3

在UILabel代碼上面添加下面的代碼。這可能對你有幫助。

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

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