【问题标题】:Cannot set text in textview in my custom tableView cell无法在我的自定义 tableView 单元格中的 textview 中设置文本
【发布时间】: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


【解决方案1】:

尝试改变这一行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];

到这一行:

CommentCell *cell = (CommentCell*)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];

【讨论】:

    【解决方案2】:

    我使用了错误的手机名称。只需将“commentCell”替换为“cmetsCell”,一切就开始工作了。

    [commentsCell.comments setText:[NSString stringWithFormat:@"%@",[[commentsArray objectAtIndex:indexPath.row] objectAtIndex:commentIndex]]];`enter code here`
    

    【讨论】:

      【解决方案3】:

      如果您不需要滚动文本视图禁用滚动以显示文本。

      我不确定这是否是由于 tableview 和 textview 都是 UIScrollView 的后代,但它可能是。

      当我遇到同样的问题时它帮助了我

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多