开发者

tableview text over ride with previous text

i am displaying array of data in a table view.

my code for that is like this.

- (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.backgroundCo开发者_运维问答lor = [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;
} 

how can i resolve this.

can any one please help me.

Thank u in advance.

i am giving text for itemNameLabel is using array and for quantityLabel using class object.

so,when i scrolling table view the array names are override on other label,but quantity label is not overriding because it is from class.

i need to display itemNameLabel from array since it changes it value.

when i scroll table view my tableview is looking like this.

tableview text over ride with previous text


Ideally you should alloc all lable in this section

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

and in the else part you should just Tag those labels and reuse after it but there is another short and sweet way if Application is not much resource consuming

change

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

to

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];


Try setting Height for each row using the function

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

    return 100.0f;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜