UITeViewView里面的UITextView [英] UITextView inside UITableView

查看:97
本文介绍了UITeViewView里面的UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经问过这个问题,虽然我似乎无法找到我想要的东西。我的应用程序中有一个部分,其中有一个带有textview的tableview。我不想为tableview单元格提供单独的.xib,.h和.m文件。 tableview不需要缩小或增长,具体取决于textview中的文本量。我也不希望textview可以编辑。我希望这不是太多要求,虽然我现在真的陷入困境。

I know this question has been asked before, though I can't seem to find what I want. I have a section in my app where I have a tableview with a textview inside of it. I DO NOT want to have a seperate .xib, .h, and .m files for the tableview cell. The tableview does not need to shrink or grow depending on the amount of text inside the textview. I don't want the textview to be editable either. I hope this isn't too much to ask for, though I'm really stuck at the moment.

推荐答案

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

To do this, you will need to embed one in your UITableViewCell. But there's no need to create a custom cell. Here is the basic idea of what you will want to do:

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

如果你不这样做,你当然需要设置你的rowHeight想要电池附带的标准44pt高度。如果你想要实际的单元格,你需要添加自己的逻辑,这样只有你想要的单元格才是textView,但这是基本的想法。其余的是你自己定制适合你的配件。希望这会有所帮助

You will, of course, need to set your rowHeight if you don't want the standard 44pt height that comes with the cell. And if you want actual cells, you'll need to add your own logic so that only the cell you want is a textView, but this is the basic idea. The rest is yours to customize to your fitting. Hope this helps

编辑:绕过textView到达你的单元格,有两种方法可以解决这个问题。

to bypass the textView to get to your cell, there are two ways to go about this.

1)你可以制作一个自定义的textView类并覆盖touchesBegan将消息发送到super:

1) you can make a custom textView class and overwrite touchesBegan to send the message to super:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
}

这会将触摸事件发送到其superview,这将是你的tableView。考虑到你不想制作自定义UITableViewCells,我想你可能也不想制作一个自定义的textView类。这导致我选择二。

this will send the touch events to its superview, which would be your tableView. Considering you didn't want to make custom UITableViewCells, I imagine you probably don't want to make a custom textView class either. Which leads me to option two.

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

2) when creating the textView, remove comment.editable = NO;. We need to keep it editable, but will fix that in a delegate method.

在你的代码中,你需要插入一个textView委托方法,我们将全部完成我们从那里开始工作:

In your code, you will want to insert a textView delegate method and we'll do all our work from there:

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

changing this code to use with a 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;
}

如果那不是你想要的,请告诉我,我们会得到的你整理了

If that's not what you wanted, just let me know and we'll get you sorted out

这篇关于UITeViewView里面的UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆