view + tableview 需要手动拖拽增加datasource和delegate 否则使用tableviewcontroller比较方便
1.显示tableviewcell
image text detail text
UITableViewCellStyleDefault 显示image + text
UITableViewCellStyleSubtitle 显示image + text + (换行)detail text
UITableViewCellStyleValue1 显示image + text + detail text
UITableViewCellStyleValue2 显示text + detail text
如果这些内置的cell style无法满足,可以重新定义table view cell
2.cellForRowAtIndexpath
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
前者MRC,后者ARC
具体使用方式不同之处在于
1.需要判断是否返回的cell为nil,如果为nil,需要做相应的处理
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
2.则将if判断cell是否为nil省略,但是需要提前注册一个cell的class
一般可以在ViewDidLoad中注册
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[BIDNameAndColorCell class]
forCellReuseIdentifier:CellTableIdentifier];
后续就可以直接使用了
BIDNameAndColorCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellTableIdentifier
forIndexPath:indexPath];
3.- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
这个函数主要定义了cell中view的呈现形式
3.设置inset,屏蔽掉电源部分,以及滚动时,遮住电源部分
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
barBackground.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.9];
[self.view addSubview:barBackground];