Iphone 慢速滚动UITableView?
我的UITableView在将消息(内容)加载到单元格后,在滚动时会出现非常明显的延迟,有时会冻结几秒钟。这很奇怪,因为所有消息都是在用户滚动时加载的。关于如何使这个快速滚动没有问题,有什么想法吗 谢谢大家!Iphone 慢速滚动UITableView?,iphone,performance,uitableview,sdk,scroll,Iphone,Performance,Uitableview,Sdk,Scroll,我的UITableView在将消息(内容)加载到单元格后,在滚动时会出现非常明显的延迟,有时会冻结几秒钟。这很奇怪,因为所有消息都是在用户滚动时加载的。关于如何使这个快速滚动没有问题,有什么想法吗 谢谢大家! - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentif
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"MailCell";
MailCell *cell = (MailCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MailCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
// Anything that should be the same on EACH cell should be here.
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:40.0/255.0 green:148.0/255.0 blue:196.0/255.0 alpha:1];
cell.selectedBackgroundView = myBackView;
cell.messageText.textAlignment = NSTextAlignmentLeft;
cell.messageText.lineBreakMode = NSLineBreakByTruncatingTail;
}
NSUInteger row = [indexPath row];
// Extract Data
// Use the message object instead of the multiple arrays.
CTCoreMessage *message = [[self allMessages] objectAtIndex:row];
// Sender
CTCoreAddress *sender = [message sender];
NSString *senderName = [sender name];
// Subject
NSString *subject = [message subject];
if ([subject length] == 0)
{
subject = @"(No Subject)";
}
// Body
BOOL isPlain = YES;
NSString *body = [message bodyPreferringPlainText:&isPlain];
body = [[body componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]]
componentsJoinedByString:@" "];
body = [body stringByReplacingOccurrencesOfString:@" " withString:@" "];
// Populate Cell
[[cell nameText] setText:senderName];
[[cell subjectField] setText:subject];
[[cell messageText] setText:body];
if ([message isUnread])
{
cell.nameText.textColor = [UIColor colorWithRed:15.0/255.0 green:140.0/255.0 blue:198.0/255.0 alpha:1];
}
else
{
cell.nameText.textColor = [UIColor blackColor];
}
return cell;
}
您的代码似乎没有问题。我建议使用一个表优化框架,比如免费的Sensive TableView。xCode附带了一个名为Instruments的分析器。它的CPU时间分析器非常适合找出哪些代码正在减慢速度。用档案器运行你的应用程序,花几秒钟的时间滚动一下。它会给你统计数字 请记住,
if(cell==nil)
中的代码将运行大约10次(UITableView缓存的单元格刚好足以填充自身)。但是if之外的代码是昂贵的——它在每次单元格可见时都会运行
我猜您发布的代码中最昂贵的操作是:
给iOS太多子视图,无法在单元格上绘制
- 自己画吧
- 您发布的代码为每个单词分配新字符串,并添加一个数组来保存它们。然后再分配两个副本(一个是单词重新连接的副本,另一个是压缩的空格)。它处理整个正文文本字符串,即使用户在正文的微小预览中永远看不到大部分李>
- 缓存生成的字符串,以便每个单元格仅执行一次此操作李>
- 此外,您还可以创建一个新的可变字符串,在其中保留空间,并在循环中复制原始字符串中的字符(空白运行除外)。您可以在100个字符左右停止处理整个正文文本(足以填充一个表格单元格)。速度更快,节省内存