gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:24:53 25 4
gpt4 key购买 nike

我正在使用 Parse 作为后端的 viewController 来充当聊天室。 cellForRowAtIndexPath 仅针对最初可见的单元格调用(因此 7 或 8 次),然后当我向下滚动 TableView 时,空间就在那里,但全是空白,并且 cellForRowAtIndexPath 不再被调用。数组的计数是正确的并且所有数据均已检索。最令人困惑的部分是,有时它在第一次加载时工作正常,但有时却出现问题。先谢谢您的帮助。这是我的 tableView 委托(delegate)方法:

#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;
}

最佳答案

cellForRowAtIndexPath 仅被调用以显示可见行。它不会被每一行调用。您可以拥有 1000 行,但只会针对屏幕上的行调用此方法。

关于ios - 绘制初始可见单元格后,cellForRowAtIndexPath 将停止调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com