2011-03-21 64 views
1

我在表格视图中显示数据数组。tableview text over previous text

我的代码就是这样的。

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

    static NSString *CellIdentifier = @"Cell"; 

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

    // Set up the cell 


    CGRect labelFrame = CGRectMake(0,0 , 100, 25);  
    itemNameLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease]; 

    itemNameLabel.textAlignment = UITextAlignmentLeft; 
    itemNameLabel.backgroundColor = [UIColor clearColor]; 
    itemNameLabel.text = [tableData objectAtIndex:indexPath.row]; 
    CGRect anotherLabelFrame = CGRectMake(120, 0, 100, 25); 
    quantityLabel = [[[UILabel alloc] initWithFrame:anotherLabelFrame] autorelease]; 

    quantityLabel.textAlignment = UITextAlignmentLeft; 
    quantityLabel.backgroundColor = [UIColor clearColor]; 
    MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    Category *obj = (Category *)[appDelegate.itemsList objectAtIndex:indexPath.row]; 

    quantityLabel.text = [NSString stringWithFormat:@"%d",obj.quantity]; 
    [cell.contentView addSubview:itemNameLabel]; 
    [cell.contentView addSubview:quantityLabel]; 


    return cell; 
} 

我该如何解决这个问题。

任何人都可以帮助我。

谢谢你提前。

我给itemNameLabel的文本是使用数组和使用类对象的quantityLabel。

所以,当我滚动表视图的数组名称覆盖其他标签,但数量标签不重写,因为它是从类。

我需要从数组中显示itemNameLabel,因为它改变了它的值。

当我滚动表视图我的tableview看起来像这样。 mytableview

回答

2

理想情况下,你应该的Alloc在本节

if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

所有拉布勒,并在其他部分,你应该只是标签的标签,之后再利用,但还有另外一个简短而亲切的方式 如果应用程序没有太大资源消耗

变化

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 
0

使用功能

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

    return 100.0f; 
} 
+0

每一行尝试设置身高有文字的重叠和其细胞的身高之间没有任何关系...... – 2011-03-21 09:11:18