我正在做一个聊天应用程序。在我的数据源方法中,我使一个单元出队并传递一个核心数据实体以对其进行初始化。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Post *post = self.posts[indexPath.row];
AbstractPostCell *cell;
if ([post.type isEqualToString:@"text"]) {
cell = [tableView dequeueReusableCellWithIdentifier:CELL_POST_LIST_TEXT forIndexPath:indexPath];
}
else if ([post.type isEqualToString:@"image"]) {
cell = [tableView dequeueReusableCellWithIdentifier:CELL_POST_LIST_IMAGE forIndexPath:indexPath];
}
[cell setupWithPost:post];
return cell;
}
这是setupWithPost方法:
- (void)setupWithPost:(Post *)post {
self.label.text = post.text;
self.datetimeLabel.text = [Format stringWithDatetime:post.datetime];
if ([DBHelper didLikePost:post likerID:[Helper currentUserID]]) {
self.likeButton.enabled = NO;
self.likeButton.titleLabel.alpha = 0.5;
self.likeButton.titleLabel.text = [NSString stringWithFormat:@"liked"];
}
else {
self.likeButton.enabled = YES;
self.likeButton.titleLabel.alpha = 1.0;
self.likeButton.titleLabel.text = [NSString stringWithFormat:@"like"];
}
}
当我第一次输入ListViewController(TableViewController)时,按钮标签是正确的,但是当我导航到下一个ViewController(DetailViewController),然后又向后导航时,所有按钮标签都未设置(显示在情节提要中预设的任何文本)
有趣的是,当我向下滚动然后向后滚动时,所有按钮标签再次变为正确。我已经尝试了prepareForReuse但没有用
注意:这仅适用于按钮标题标签,普通标签(例如datetimeLabel)始终正确显示
- (void)prepareForReuse {
[super prepareForReuse];
self.label.text = @"";
self.datetimeLabel.text = @"";
self.likeButton.titleLabel.text = @"";
}
听起来很奇怪,我不知道这是否是问题的原因,但您应该使用API设置按钮标题
- (void)setTitle:(NSString *)title forState:(UIControlState)state
尝试
[self.likeButton setTitle:@"some title" forState:UIControlStateNormal];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句