【发布时间】:2016-09-27 06:31:51
【问题描述】:
我正在尝试使用数据在 UItableViewCell 上加载我的 UITextView,但无法设置文本。 UITextViewDelegate 也被设置并附加到视图控制器。文本字符串不为空,因为我使用调试器进行了检查。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
//CommentCell is my custom cell with textView.
CommentCell *commentsCell = (CommentCell*)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CommentCellIdentifier];
}
//Setting the images for all buttons in service row.
[commentsCell.deletecomment setImage:deleteComment forState:UIControlStateNormal];
[commentsCell.editcomment setImage:editComment forState:UIControlStateNormal];
commentsCell.deletecomment.layer.borderColor = [UIColor blackColor].CGColor;
commentsCell.editcomment.layer.borderColor = [UIColor blackColor].CGColor;
NSInteger commentIndex = 2;
//CommentsArray is NSArray with data.
[commentCell.comments setText:[NSString stringWithFormat:@"%@",[[commentsArray objectAtIndex:indexPath.row] objectAtIndex:commentIndex]]];
return cell;
}
【问题讨论】:
-
您是否已注册该单元格或将其添加到情节提要中?您应该在
if (cell == nil) { ..}内设置断点并查看它是错误的 -
最好使用自定义单元格
-
是的。我将它添加到情节提要中并将其分配给我的自定义单元格类。不需要注册,因为我在其他视图控制器中也是如此。唯一的区别是我使用的是 UITextView 而不是 UIlabel。
-
单元格已注册并且不是假的,我正在使用自定义单元格,如您所见,我正在设置图像并且它们设置正确。
-
1)
dequeueReusableCellWithIdentifier:forIndexPath:不能返回nil,所以下面的if语句是没用的。 2) 无论如何,if语句是错误的,因为它创建了一个通用的UITableViewCell。删除if块。
标签: ios objective-c xcode ios7