制作自定义单元格布局确实需要一些编码,所以我希望这不会吓到您。
第一件事是创建一个新的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
.
祝你好运!