【问题标题】:UITextView into UITableViewCellUITextView 变成 UITableViewCell
【发布时间】:2010-12-07 21:36:53
【问题描述】:

我是菜鸟。我需要通过动态调整大小将 UITextView 插入到 UITableViewCell 中,并且我会直接在单元格中输入。请帮我解决这个问题。

【问题讨论】:

  • 您应该将 Rog 的回答标记为已接受,以便关闭此问题。 (您和 Rog 都将获得声望点数。)

标签: objective-c uitableview insert resize uitextview


【解决方案1】:

您需要使用 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;
}

【讨论】:

  • +1 不错的参考实现。 (对于初学者来说可能有点多,但有一些目标。)
  • 不用担心@Constantine。如果您对答案感到满意,请将问题标记为已回答,因为它有助于提高我和您在社区中的声誉。谢谢!
【解决方案2】:

这不是标准 UITableView 的设计方式。如果这是您想要实现的目标,则有一种添加/编辑/删除项目的定义方式。

我建议您仔细阅读 Table View Programming Guide for iOS(特别是“插入和删除行和节”部分),因为这会让您走上正轨。

如果您真的希望允许用户在单元格中输入内容,当然可以创建自定义视图等,但作为一个自称“菜鸟”的人,我不建议您尝试这样做直到您对上述方法更有信心,等等。

【讨论】:

  • 我不一定同意这一点。如果您正在实现一个供用户输入的表单怎么办?带有 UITextFields 的 tableview 绝对是一个很好的解决方案,事实上 Apple 经常使用它(例如,当您在 iPhone 上添加新联系人时)。
  • 我只是在想,对于初学者来说,在创建自定义视图之前了解标准的做事方式将是一个很好的起点。
  • 初学者应该学习标准(正确)的做事方式。但是,当他们提出更高级的问题时,我们会为他们提供更高级的答案。
  • @W Dyson - SO 的优点是 OP 现在在一个页面上有很好的链接。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多