views:

13

answers:

1

I am adding an activity indicator in each row of table. The issue is every time I scroll it get added again in cell overwriting the previous one. Please let me know which is best way to add control in tableview cell.

UIActivityIndicatorView *a;

// Customize the appearance of table view cells.

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [a startAnimating];
    [cell.contentView addSubview:a];

    // Configure the cell.

    return cell;
}
A: 

Add indicator to a cell only when you create it, then you can access it using its tag property:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    a.tag = 10;
    [cell.contentView addSubview:a];
}
UIActivityIndicatorView* aView = [cell.contentView viewWithTag:10];
[aView startAnimating];
Vladimir
Thanks a lot exactly this "[cell.contentView viewWithTag:10]" I wanted to know. thanks again
iPhoneDev