预定义cell
// Designated initializer. If the cell can be reused, you must pass in a reuse identifier. You should use the same reuse identifier for all cells of the same form.
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0) NS_DESIGNATED_INITIALIZER;
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1, // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2, // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
}; // available in iPhone OS 3.0
4种style的UITableViewCell,可以设置的内容有
// Content. These properties provide direct access to the internal label and image views used by the table view cell. These should be used instead of the content properties below.
@property (nonatomic, readonly, strong, nullable) UIImageView *imageView NS_AVAILABLE_IOS(3_0); // default is nil. image view will be created if necessary.
@property (nonatomic, readonly, strong, nullable) UILabel *textLabel NS_AVAILABLE_IOS(3_0); // default is nil. label will be created if necessary.
@property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel NS_AVAILABLE_IOS(3_0); // default is nil. label will be created if necessary (and the current style supports a detail label).
static NSString *cellId = @"cellId";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
自定义cell
纯代码
// Designated initializer. If the cell can be reused, you must pass in a reuse identifier. You should use the same reuse identifier for all cells of the same form.
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0) NS_DESIGNATED_INITIALIZER;
// If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
@property (nonatomic, readonly, strong) UIView *contentView;
继承UITableViewCell,override initWithStyle:reuseIdentifier,对contentView定制
@interface FBTableViewCell : UITableViewCell
@end
@implementation FBTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
//implement code, configure self.contentView
}
return self;
}
@end
FBTableViewCell就可以像UITableViewCell一样使用
static NSString *cellId = @"cellId";
UITableViewCell *cell = [[FBTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
storyboard——dynamic prototypes
在storyboard的UITableView中设置Content为Dynamic ProtoTypes,设置ProtoType Cells的数量,就可以进行图形化设计自定义UITableViewCell了,这些自定义UITableViewCell会被加入UITableView的reusable cell dequeue中,所有在设计这些自定义UTableViewCell时要指定identifier,如果需要动态访问UITableViewCell中的子控件,通过设置子控件的tag访问,这种自定义UTableViewCell完全通过图形化方式完成,不需要一行代码
使用方式
UITableViewCell * cell = [tblView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
xib
代码和xib混合使用,需要定义UITableViewCell子类,然后绑定该子类到xib文件,如果xib中的子控件需要动态访问,UITableViewCell子类中可以添加IBOutlet
使用方法:
static NSString *cellId = @"cellId";
static BOOL isRegist = NO;
if(!isRegist)
{
UINib *nib = [UINib nibWithNibName:@"FBTableViewCell" bundle:nil];
[tblView registerNib:nib forCellReuseIdentifier:cellId];
isRegist = YES;
}
FBTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
storyboard——static cells
static cells的本质就是抛弃UITableViewDataSource的定制,UITableView不再需要实现定制UITableViewDataSource的任何方法,直接在storyboard中设计完成,如果需要动态访问static cells的子控件,可以绑定到UITableViewController的IBOutlet