And*_*y B 6 iphone uitableview uitextview
我知道之前已经问过这个问题,虽然我似乎无法找到我想要的东西.我的应用程序中有一个部分,其中有一个带有textview的tableview.我不想为tableview单元格分别创建.xib,.h和.m文件.tableview不需要缩小或增长,具体取决于textview中的文本量.我也不希望textview可以编辑.我希望这不是太多要求,虽然我现在真的被困住了.
jus*_*tin 24
为此,您需要在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;
}
Run Code Online (Sandbox Code Playgroud)
当然,如果您不想要单元格附带的标准44pt高度,您将需要设置rowHeight.如果你想要实际的单元格,你需要添加自己的逻辑,这样只有你想要的单元格才是textView,但这是基本的想法.其余的是你自己定制适合你的配件.希望这可以帮助
编辑:绕过textView进入你的单元格,有两种方法可以解决这个问题.
1)你可以创建一个自定义textView类并覆盖touchesBegan将消息发送到super:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
这会将触摸事件发送到它的superview,这将是你的tableView.考虑到你不想制作自定义UITableViewCells,我想你可能也不想制作一个自定义的textView类.这导致我选择二.
2)创建textView时,删除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;
}
Run Code Online (Sandbox Code Playgroud)
如果这不是你想要的,请告诉我,我们会帮你解决
归档时间: |
|
查看次数: |
13730 次 |
最近记录: |