【发布时间】:2009-07-03 08:37:26
【问题描述】:
我正在编写一个线程化的 UITableView,它可以从网上检索一些信息。我想在加载图像时显示一个活动指示器来代替常规的 cell.image。
问题在于自定义标签和活动指示器的行为不正常。标签从正确的位置开始,活动指示器显示在正确的位置并开始动画,但是一旦在图像下载线程上遇到第一个回调(基本上只加载一个图像),视图全部就像只有一个参考一样重置,我正在重置所有这些的框架......
我已经重载了 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
有问题的代码在这里:
// 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)];
}
}
iPhone 应用程序开发的新手,不到两周。我想知道我是否误解了标签的使用?在我看来,如果您可以将标签值链接到 indexPath,您可以想出唯一标识符来引用自定义视图? 100 和 200 是任意值,这样我就不会意外地在一屏数据中重复使用标签——不知道是否有必要,因为我认为标签只在子视图中搜索。
提前感谢您的任何建议。还可以接受有关如何使其更优雅的一般提示...
谢谢, 乔什
【问题讨论】:
标签: iphone uitableview