Hello Folks,
I was in the process of writing a threaded UITableView that retrieves some information from the net. I would like to display an activity indicator in place of the regular cell.image while the images are loading.
The problem is that the custom label and activity indicators are not behaving properly. The labels start out in the proper position, the activity indicator shows up in the proper position and begins to animate, but as soon as the first callback is hit on the image download thread (only one image is loaded, basically), the views all reset as if there were a single reference and I was resetting the frame on all of them...
I have overloaded - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
The code in question is here:
// Set up the cell...
if( ![[cell contentView] viewWithTag:indexPath.row+200] ){
UILabel *newLabel = [[UILabel alloc] init];
newLabel.text = [[personList objectAtIndex:indexPath.row] name];
newLabel.frame = [[cell contentView] frame];
[[cell contentView] addSubview:newLabel];
newLabel.tag = indexPath.row+200;
[newLabel release];
}
else{
UILabel *alreadySetLabel = (UILabel *)[[cell contentView] viewWithTag:indexPath.row+200];
alreadySetLabel.text = [[personList objectAtIndex:indexPath.row] name];
alreadySetLabel.frame = [[cell contentView] frame];
}
UIImageView *tmpImageView = cell.imageView;
NSLog( @"In cell name: %@", [[personList objectAtIndex:indexPath.row] name] );
NSLog( @"In cell profile image: %@", [[personList objectAtIndex:indexPath.row] profileImage] );
if( [[personList objectAtIndex:indexPath.row] profileImage] ){
tmpImageView.image = [[personList objectAtIndex:indexPath.row] profileImage];
[[[cell contentView] viewWithTag:indexPath.row+100] removeFromSuperview];
UILabel *labelRef1 = (UILabel *)[[cell contentView] viewWithTag:indexPath.row+200];
[labelRef1 setFrame:CGRectMake( cell.contentView.frame.origin.x+70, cell.contentView.frame.origin.y, labelRef1.frame.size.width, labelRef1.frame.size.height)];
}
else{
NSLog( @"%@", [[personList objectAtIndex:indexPath.row] imageFilePath] );
if( ![[cell contentView] viewWithTag:indexPath.row+100] && [[personList objectAtIndex:indexPath.row] imageFilePath] ){
UIActivityIndicatorView *newSpin = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[newSpin setTag:indexPath.row+100];
[newSpin startAnimating];
[newSpin setFrame:CGRectMake( 5, ((cell.contentView.frame.size.height/2)-7), 30, ((cell.contentView.frame.size.height/2)-7)+15) ];
[[cell contentView] addSubview:newSpin];
[newSpin release];
UILabel *labelRef2 = (UILabel *)[cell viewWithTag:indexPath.row+200];
[labelRef2 setFrame:CGRectMake( cell.contentView.frame.origin.x+70, cell.contentView.frame.origin.y, labelRef2.frame.size.width, labelRef2.frame.size.height)];
}
}
Brand new to iphone application development, less than two weeks in. I'm wondering if I am misunderstanding the use of tags? It seemed to me like if you could link the tag value to the indexPath, you could come up with unique identifiers to refer to the custom views? 100 and 200 are arbitrary values, such that I won't accidentally reuse the tags within a single screenful of data -- don't know if thats necessary, as I thought that tags only searched through the subviews.
Thanks in advance for any advice. Also open to general tips on how to make this more elegant...
Thanks, Josh