UITextView 到 UITableViewCell

发布于 2024-10-06 09:06:02 字数 81 浏览 0 评论 0原文

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

I'am noob. I need insert UITextView in to UITableViewCell with dynamic resizing and that i would typing right into cell. Please help me resolve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-10-13 09:06:02

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

You need to subclass UITableViewCell with a UITextField in it:

@interface CustomCell : UITableViewCell {
    UILabel *cellLabel;
    UITextField *cellTextField;
}

@property (nonatomic, retain) UILabel *cellLabel;
@property (nonatomic, retain) UITextField *cellTextField;

@end

And then implement:

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

And finally use your custom cells on:

- (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;
}
谎言 2024-10-13 09:06:02

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

我建议仔细阅读 iOS 表格视图编程指南(特别是“插入和删除行和节”部分),因为这将使您走上正轨。

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

This isn't how the standard UITableView is designed to work. There's a defined way of adding/editing/removing items if that's what you're trying to achieve.

I'd recommend having a good read of the Table View Programming Guide for iOS (specifically the "Inserting and Deleting Rows and Sections" section) as this will put you on the right track.

You could of course create a custom view, etc. if you really want to allow users to type into a cell, but as a self-confessed "noob" I wouldn't recommend attempting this until you're more confident with the above approach, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文