我正在編寫一個線程化的UITableView,它從網上檢索一些信息。我想在加載圖像時顯示活動指示符以代替常規cell.image。你如何在UITableViewCell中重新定位子視圖?
問題是自定義標籤和活動指示器表現不正常。標籤從適當的位置開始,活動指示器顯示在適當的位置並開始生成動畫,但只要在圖像下載線程上初次回調(基本上只加載一個圖像),視圖全部重置,就好像有一個參考,我被重置在一切的框架...
我重載 - (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是任意值,這樣我就不會意外地在單個屏幕數據中重複使用標籤 - 不知道這是否必要,因爲我認爲標籤只能通過子視圖搜索。
在此先感謝您的任何建議。也歡迎就如何使這更優雅的一般技巧...
感謝, 喬希