我在viewController上工作,作为聊天室使用解析作为后端。只有最初可见的单元格才会调用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;
}发布于 2013-12-14 08:30:36
调用cellForRowAtIndexPath只是为了显示可见的行。并不是每一行都调用它。您可以有1000行,但这将只为屏幕上的行调用。
https://stackoverflow.com/questions/20578293
复制相似问题