可编辑的UITableView,每个单元格上都有一个文本字段

Zak*_*001 5 objective-c uitableview ios

我是iOS世界的新手,我想知道如何制作一个UITableView自定义单元格,其外观和行为与您尝试在设备上配置某些WiFi连接时的单元格相似.(你知道UITableView包含UITextField带蓝色字体的s的单元格,你设置了ip地址和所有东西......).

小智 9

制作自定义单元格布局确实涉及一些编码,所以我希望不要吓到你.

首先是创建一个新的UITableViewCell子类.我们称之为InLineEditTableViewCell.您的界面InLineEditTableViewCell.h可能如下所示:

#import <UIKit/UIKit.h>

@interface InLineEditTableViewCell : UITableViewCell

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UITextField *propertyTextField;

@end
Run Code Online (Sandbox Code Playgroud)

InLineEditTableViewCell.m可能看起来像这样:

#import "InLineEditTableViewCell.h"

@implementation InLineEditTableViewCell

@synthesize titleLabel=_titleLabel;
@synthesize propertyTextField=_propertyTextField;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Here you layout your self.titleLabel and self.propertyTextField as you want them, like they are in the WiFi settings.
    }
    return self;
}

- (void)dealloc
{
    [_titleLabel release], _titleLabel = nil;
    [_propertyTextField release], _propertyTextField = nil;
    [super dealloc];
}

@end
Run Code Online (Sandbox Code Playgroud)

接下来就是你UITableView在视图控制器中正常设置你的设置.执行此操作时,您必须实现UITablesViewDataSource协议方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.在为此插入实现之前,请记住#import "InLineEditTableViewCell"在视图控制器中.执行此操作后,实现如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InLineEditTableViewCell *cell = (InLineEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"your-static-cell-identifier"];

    if (!cell) {
        cell = [[[InLineEditTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"your-static-cell-identifier"] autorelease];
    }

    // Setup your custom cell as your wish
    cell.titleLabel.text = @"Your title text";
}
Run Code Online (Sandbox Code Playgroud)

而已!你现在有自定义单元格UITableView.

祝好运!

  • 谢谢你的帮助.其实我不怕编码,我不擅长操纵图形元素:) (3认同)