IPhone UITableView在滚动时会抖动
我将表从XML提要加载到数组中,然后创建单元格,如下面的代码所示。有时在装载时,它会非常不平稳。当我滚动时,我以为那只是下载的图像,但我把它们移到了测试中,有时还是很不稳定。不知道我做错了什么IPhone UITableView在滚动时会抖动,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我将表从XML提要加载到数组中,然后创建单元格,如下面的代码所示。有时在装载时,它会非常不平稳。当我滚动时,我以为那只是下载的图像,但我把它们移到了测试中,有时还是很不稳定。不知道我做错了什么 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { const NSInteger BOTTOM_LABEL_TAG = 1002;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger BOTTOM_LABEL_TAG = 1002;
const NSInteger TEXT_FIELD_TAG = 1003;
UILabel *Labelcompanyname;
UILabel *Labeldistance;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell =
[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]
autorelease];
const CGFloat LABEL_HEIGHT = 15;
int spaceSize;
if(currentType == @"categorySearch"){
spaceSize = 57;
}
else {
spaceSize = 34;
}
Labelcompanyname =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
spaceSize + 2.0 * cell.indentationWidth,
0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT)+10,
tableView.bounds.size.width -
spaceSize - 20.0,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:Labelcompanyname];
Labeldistance =[[[UILabel alloc]
initWithFrame:
CGRectMake(
spaceSize + 2.0 * cell.indentationWidth,
0.5 * (tableView.rowHeight - 2 * LABEL_HEIGHT)+40,
tableView.bounds.size.width -
spaceSize - 20.0,
LABEL_HEIGHT)]
autorelease];
if(currentType == @"categorySearch") {
[cell.contentView addSubview:Labeldistance];
}
Labelcompanyname.tag = BOTTOM_LABEL_TAG;
Labelcompanyname.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
Labeldistance.tag = TEXT_FIELD_TAG;
Labeldistance.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
cell.textLabel.numberOfLines=0;
}
else
{
Labelcompanyname = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
Labeldistance = (UILabel *)[cell viewWithTag:TEXT_FIELD_TAG];
}
PromotionObject *newObj1;
newObj1=[totalArray objectAtIndex:indexPath.row];
if(!newObj1.furtherURL){
Labelcompanyname.text =[NSString stringWithFormat:@"%@", newObj1.companyname];
Labeldistance.text =newObj1.distance;
}
else
{
Labelcompanyname.text =[NSString stringWithFormat:@"Load more results"];
Labeldistance.text =@"";
}
NSURL *Imageurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", newObj1.locImage]];
NSData *data = [NSData dataWithContentsOfURL:Imageurl];
UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
if(currentType == @"categorySearch"){
cell.imageView.image = img;
} else if(currentType == @"fullSearch") {
cell.imageView.image = [UIImage imageNamed:@"newmap.png"];
} else {
cell.imageView.image = [UIImage imageNamed:@"business.png"];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
任何帮助都将不胜感激
Tom在加载单元格时,您绝对不应该做任何在很长时间内阻塞UI线程的事情。例如,
NSData*data=[NSData dataWithContentsOfURL:Imageurl]代码>
我建议将图像加载到后台的数据源中,并在下载数据后重新加载/更新表/单元格。为你的图像实现一个缓存也很好,这样你就不必每次加载单元格时都再次下载它们
如果您需要异步图像加载方面的帮助,请搜索。在加载单元格时,你绝对不应该做任何在很长时间内阻塞UI线程的事情。例如,NSData*data=[NSData dataWithContentsOfURL:Imageurl]代码>
我建议将图像加载到后台的数据源中,并在下载数据后重新加载/更新表/单元格。为你的图像实现一个缓存也很好,这样你就不必每次加载单元格时都再次下载它们
如果您需要异步图像加载方面的帮助,请搜索。这里面有很多东西。我想你自己也得到了答案。图像加载会阻止UI完成当前单元格请求<代码>表格视图:cellforrowatinexpath:
在获取图像之前无法返回。我建议您用活动指示器替换图像,并开始在背景中获取图像。你可能想看看我想你自己也得到了答案。图像加载会阻止UI完成当前单元格请求<代码>表格视图:cellforrowatinexpath:
在获取图像之前无法返回。我建议您用活动指示器替换图像,并开始在背景中获取图像。你可能想看一看如果你使用它的imageView类别将为你处理所有这些感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢感谢,我将查看它+1,因为afnetworking.org看起来非常漂亮,非常有用。