【发布时间】:2010-12-07 21:36:53
【问题描述】:
我是菜鸟。我需要通过动态调整大小将 UITextView 插入到 UITableViewCell 中,并且我会直接在单元格中输入。请帮我解决这个问题。
【问题讨论】:
-
您应该将 Rog 的回答标记为已接受,以便关闭此问题。 (您和 Rog 都将获得声望点数。)
标签: objective-c uitableview insert resize uitextview
我是菜鸟。我需要通过动态调整大小将 UITextView 插入到 UITableViewCell 中,并且我会直接在单元格中输入。请帮我解决这个问题。
【问题讨论】:
标签: objective-c uitableview insert resize uitextview
您需要使用 UITextField 子类化 UITableViewCell:
@interface CustomCell : UITableViewCell {
UILabel *cellLabel;
UITextField *cellTextField;
}
@property (nonatomic, retain) UILabel *cellLabel;
@property (nonatomic, retain) UITextField *cellTextField;
@end
然后执行:
@implementation CustomCell
@synthesize cellLabel, cellTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
cellLabel = [[UILabel alloc] initWithFrame:CGRectZero];
... // configure your label appearance here
cellTextField = [[UITextField alloc] initWithFrame:CGRectZero];
... // configure your textfield appearance here
}
return self;
}
最后使用您的自定义单元格:
- (CustomCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
... // configure your cell data source here
return cell;
}
【讨论】:
这不是标准 UITableView 的设计方式。如果这是您想要实现的目标,则有一种添加/编辑/删除项目的定义方式。
我建议您仔细阅读 Table View Programming Guide for iOS(特别是“插入和删除行和节”部分),因为这会让您走上正轨。
如果您真的希望允许用户在单元格中输入内容,当然可以创建自定义视图等,但作为一个自称“菜鸟”的人,我不建议您尝试这样做直到您对上述方法更有信心,等等。
【讨论】: