Iphone UITableView中的UITextView

Iphone UITableView中的UITextView,iphone,uitableview,uitextview,Iphone,Uitableview,Uitextview,我知道以前有人问过这个问题,尽管我似乎找不到我想要的。我的应用程序中有一个部分,其中有一个tableview,里面有一个textview。我不想为tableview单元格单独设置.xib、.h和.m文件。tableview不需要根据textview中的文本量进行收缩或增长。我也不希望textview是可编辑的。我希望这不是太多的要求,尽管我现在真的被卡住了。要做到这一点,您需要在UITableViewCell中嵌入一个。但不需要创建自定义单元格。以下是您想做的基本想法: - (UITableVi

我知道以前有人问过这个问题,尽管我似乎找不到我想要的。我的应用程序中有一个部分,其中有一个tableview,里面有一个textview。我不想为tableview单元格单独设置.xib、.h和.m文件。tableview不需要根据textview中的文本量进行收缩或增长。我也不希望textview是可编辑的。我希望这不是太多的要求,尽管我现在真的被卡住了。

要做到这一点,您需要在UITableViewCell中嵌入一个。但不需要创建自定义单元格。以下是您想做的基本想法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UITextView *comment = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, tableView.rowHeight)];
        comment.editable = NO;
        comment.delegate = self;
        [cell.contentView addSubview:comment];
        [comment release];
    }
    return cell;
}
当然,如果您不想要单元格附带的标准44磅高度,则需要设置行高。如果你想要实际的单元格,你需要添加你自己的逻辑,只有你想要的单元格是文本视图,但这是基本的想法。其余部分由您根据自己的喜好定制。希望这有帮助

编辑:要绕过文本视图进入您的单元格,有两种方法

1) 您可以创建自定义textView类并覆盖Touches开始向super发送消息:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
}
这会将触摸事件发送到它的superview,即您的tableView。考虑到您不想创建自定义UITableViewCells,我想您可能也不想创建自定义textView类。这让我想到了第二种选择

2) 创建文本视图时,删除
comment.editable=NO。我们需要保持它的可编辑性,但将在委托方法中修复它

在您的代码中,您需要插入一个textView委托方法,我们将从此处开始执行所有工作:

编辑:将此代码更改为与UITableViewController一起使用

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// this method is called every time you touch in the textView, provided it's editable;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:textView.superview.superview];
    // i know that looks a bit obscure, but calling superview the first time finds the contentView of your cell;
    //  calling it the second time returns the cell it's held in, which we can retrieve an index path from;

    // this is the edited part;
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    // this programmatically selects the cell you've called behind the textView;


    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    // this selects the cell under the textView;
    return NO;  // specifies you don't want to edit the textView;
}

如果这不是您想要的,请告诉我,我们会帮您解决问题

在调用cellForRowAtIndexPath时,这不总是添加一个子视图吗?UITextView不应该添加到if(cell==nil)条件块中吗?这将每次添加一个,是的。最好将其添加到if(cell==nil)块中。我为此道歉。我在我的一个应用程序的案例表单中使用了它,每次调用它更新单元格位置并提供新标记时,我都将单元格设置为nil。我想我记得为你把它改回正常状态,但错过了那个部分,所以我很高兴你抓住了它。现在我将把它编辑到正确的位置哈哈,我也犯了同样的错误:)当我的tableview像蜗牛一样滚动时,我很难理解为什么。。。干杯总是一些小事让你着迷,哈哈。不过,再次感谢你的精彩表演。我非常感激,没问题。我很高兴一切顺利,非常欢迎你