我在viewController上工作,它使用Parse作为后端充当聊天室。 cellForRowAtIndexPath仅针对最初可见的单元格调用(所以7或8次),然后当我向下滚动表格视图时,空间就在那里,但它全部为空,并且不再调用cellForRowAtIndexPath。数组的计数是正确的,并且检索所有数据。最令人困惑的部分是,有时它在第一次加载时正常工作,然后在问题发生的其他时间。在此先感谢您的帮助。这是我的tableView委托方法:
#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;
}
答案 0 :(得分:1)
仅调用cellForRowAtIndexPath以显示可见行。每一行都没有调用它。您可以拥有1000行,但只会调用屏幕上的行。