可编辑的UITableView与每个单元格上的文本字段
我是新来的iOS世界,我想知道如何使用自定义单元格来创buildUITableView
,这些单元格的外观和行为与您尝试在设备上configuration某个WiFi连接时所使用的格式相同。 (你知道UITableView
与包含UITextField
的单元格,在那里你设置了IP地址和所有的东西…)。
做一个自定义的单元格布局确实涉及到一些编码,所以我希望不要吓到你。
首先是创build一个新的UITableViewCell
子类。 我们称之为InLineEditTableViewCell
。 你的接口InLineEditTableViewCell.h
可能看起来像这样:
#import <UIKit/UIKit.h> @interface InLineEditTableViewCell : UITableViewCell @property (nonatomic, retain) UILabel *titleLabel; @property (nonatomic, retain) UITextField *propertyTextField; @end
而你的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
接下来的事情是你像你通常在你的视图控制器中设置你的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"; }
而已! 你现在有你的UITableView
自定义单元格。
祝你好运!