2

I have a UITableView in which each UITableViewCell having a UIView on them and and above UIView i have two UIButton and two UILabel. My problem is this when i am reloading the tableview then the new data is rewritten on the old data that makeks content blurred and unclear and in method cellForRowAtIndexPath i used this code of removing label then it is also not working properly.......

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UILabel *subview in subviews) {
        [subview removeFromSuperview];
    }
    [subviews release];

This code is showing a row at a time otherwise no one...

The code of cellForRowAtIndexPath is this....

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = [appDelegate.webDataList objectAtIndex:indexPath.row];

    tableView.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:0.39];

    if([appDelegate.dataArray count]>0){

        imgView.backgroundColor = [UIColor clearColor];
        imgView =[[UIView alloc ] initWithFrame:CGRectMake(0,0,320,45)];
        imgView.tag= indexPath.row+250;
        [cell.contentView addSubview:imgView];

        button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(291, 5, 19, 21);
        img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]];
        [button setBackgroundImage:img forState:UIControlStateNormal];
        [button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.tag= indexPath.row+250;
        [imgView addSubview:button];

        button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(-25, 5, 19, 21);
        img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]];
        [button setBackgroundImage:img forState:UIControlStateNormal];
        [button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.tag= indexPath.row+250;
        [imgView addSubview:button];

        button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(5, 7, 19, 21);
        button.tag = indexPath.row+250;
        img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"delete" ofType:@"png"]];
        [button setBackgroundImage:img forState:UIControlStateNormal];
        [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];    
        [imgView addSubview:button];

        NSArray *arr = [[appDelegate.dataArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"/"];


        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)];
        label.text = [arr objectAtIndex:1];
        label.tag = indexPath.row+250;
        [label setFont:[UIFont fontWithName:@"Georgia" size:14]];
        [label setNumberOfLines:0];
        label.textAlignment = UITextAlignmentLeft;
        label.textColor = [UIColor whiteColor];
        label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        label.backgroundColor = [UIColor clearColor];
        [imgView addSubview:label];
        [label release];

        NSDictionary *dict1 =[appDelegate.webDataList objectAtIndex:indexPath.row];


        label = [[UILabel alloc] initWithFrame:CGRectMake(181, 7, 75, 20)];
        label.tag = indexPath.row+250;
        label.text = [dict1 objectForKey:@"time"];
        [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]];
        label.textAlignment = UITextAlignmentLeft;
        [label setBackgroundColor:[UIColor clearColor]];
        label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [label setTextColor:[UIColor whiteColor]];
        [imgView addSubview:label];
        [label release];
    }
    return cell;
}

and in my code i am not able to use the method reuseTableViewCellWithIdentifier: because i want each label and button tag should be same because i have to fetch them else where.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
shivangi
  • 187
  • 2
  • 14

1 Answers1

3

That happens because you are doing it wrong. You don't even need to reload the table. Just scroll around and you will notice it.
You create new labels and buttons each time a cell is displayed. And those UI elements are of course never removed.

Add the UI element when you create a new cell and give them a tag. If the cell was successful dequeued get the element by that tag and set its values.

Like this:

if (cell == nil) {
    // if no cell could be dequeued create a new one
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

    // add label to the new cell
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)];
    label.tag = 250;  // <---- save tag
    [label setFont:[UIFont fontWithName:@"Georgia" size:14]];
    [label setNumberOfLines:0];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor = [UIColor whiteColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    label.backgroundColor = [UIColor clearColor];
    //[imgView addSubview:label]; // I ignore this because I don't want to write 200 lines of code here. This is just an example so please adopt the code yourself
    [cell.contentView addSubView:label];
    [label release];
}

// whatever happened you have a cell with all the labels added here

UILabel *label = (UILabel *)[cell.contentView viewWithTag:250]      // <---- get label with tag
label.text = [arr objectAtIndex:1];

and you don't need if([appDelegate.dataArray count]>0). If all your other tableview datasource methods are correct tableView:cellForRowAtIndexPath: isn't called if your data source is empty.


Edit: Just saw that one:

and in my code i am not able to use the method reuseTableViewCellWithIdentifier: because i want each label and button tag should be same because i have to fetch them else where.

Actually you can. Everybody can and should use reuseTableViewCellWithIdentifier:.
What are your exact problems with that?
If you need to get the indexPath (or in your case only the row) in the button action method use the code from my answer to another question

Community
  • 1
  • 1
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247