首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制初始可见单元格后,将停止调用cellForRowAtIndexPath

绘制初始可见单元格后,将停止调用cellForRowAtIndexPath
EN

Stack Overflow用户
提问于 2013-12-14 08:29:29
回答 1查看 452关注 0票数 0

我在viewController上工作,作为聊天室使用解析作为后端。只有最初可见的单元格才会调用cellForRowAtIndexPath (所以会调用7到8次),然后当我向下滚动表视图时,空间仍然存在,但它是空白的,并且不再调用cellForRowAtIndexPath。数组的计数是正确的,并且检索到了所有数据。最令人困惑的部分是,有时它会在第一次加载时正常工作,然后在其他时候出现问题。提前感谢你的帮助。下面是我的tableView委托方法:

代码语言:javascript
复制
#pragma mark - Table View Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.chatData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChatCell *cell = (ChatCell *)[tableView dequeueReusableCellWithIdentifier:@"chatCellIdentifier"];

NSUInteger row = [self.chatData count]-[indexPath row]-1;


if (row < [self.chatData count]) {

    NSString *chatText = [[self.chatData objectAtIndex:row] objectForKey:@"text"];
    NSString *theUserName = [[self.chatData objectAtIndex:row] objectForKey:@"userName"];
    PFFile *imageFile = [[self.chatData objectAtIndex:row] objectForKey:@"avatar"];

    UIFont *font = [UIFont fontWithName:@"Arial" size:13.0];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];

    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:chatText attributes:attributes];


    CGRect rect = [chatText boundingRectWithSize:CGSizeMake(225.0f, CGFLOAT_MAX)
                                      options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                   attributes:attributes
                                      context:nil];
    CGSize size = rect.size;

    cell.textString.frame = CGRectMake(76, 23, size.width +20, size.height + 20);
    cell.textString.textAlignment = NSLineBreakByWordWrapping;

    NSDate *theDate = [[self.chatData objectAtIndex:row] objectForKey:@"date"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm a"];
    NSString *timeString = [formatter stringFromDate:theDate];

    cell.textString.attributedText = attrString;
    [cell.textString sizeToFit];
    cell.timeLabel.text = timeString;
    cell.userLabel.text = theUserName;
    cell.userAvatar.file = imageFile;
}


return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [[self.chatData objectAtIndex:self.chatData.count-indexPath.row-1] objectForKey:@"text"];

UIFont *cellFont = [UIFont fontWithName:@"Arial" size:13.0];
CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);

CGRect boundingRect = [cellText boundingRectWithSize:constraintSize
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:[NSDictionary dictionaryWithObjectsAndKeys:cellFont, NSFontAttributeName, nil]
                                         context:nil];

return boundingRect.size.height + 40;
}
EN

回答 1

Stack Overflow用户

发布于 2013-12-14 08:30:36

调用cellForRowAtIndexPath只是为了显示可见的行。并不是每一行都调用它。您可以有1000行,但这将只为屏幕上的行调用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20578293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档