使用视图文件自定义较复杂的UITableViewCell
,之前的做法是先做一个UITableViewCell
的子类,然后再做一个以uitableviewcell
为view
的viewcontroller
。后有了storyboard
,在里面UITableView
的content
有两个属性Dynamic Prototypes
和static cells
,至于这两个的区别,Dynamic Prototypes
属性支持重用,static cells
就不介绍了,它连UITableViewDataSource
的方法都不用。
设置UITabelView
![image](/images/DynamicPrototypesTable/Screen Shot 2014-02-10 at 11.29.51 AM.png)
向UITabelView添加cell和cell的内容
![image](/images/DynamicPrototypesTable/Screen Shot 2014-02-12 at 5.26.38 PM.png)
这步很爽有木有。然后设置不同样式cell
的Identifier
![image](/images/DynamicPrototypesTable/Screen Shot 2014-02-10 at 11.42.23 AM.png)
UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCellId;
if (indexPath.row == 0) {
strCellId = @"cellSlider";
}else if (indexPath.row == 1){
strCellId = @"cellSegmented";
}else{
strCellId = @"cellSwitch";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellId];
return cell;
}
![image](/images/DynamicPrototypesTable/iOS Simulator Screen shot Feb 12, 2014, 5.33.17 PM.png)
cell内的信息设置
做一个UITableViewCell
子类
![image](/images/DynamicPrototypesTable/Screen Shot 2014-02-10 at 11.42.05 AM.png)
UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCellId;
if (indexPath.row == 0) {
strCellId = @"cellSlider";
}else if (indexPath.row == 1){
strCellId = @"cellSegmented";
}else{
strCellId = @"cellSwitch";
}
DynamicPrototypesCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellId];
//set cell source
if ([strCellId isEqualToString:@"cellSlider"]) {
cell.labelFlag.text = @"slider";
}else if([strCellId isEqualToString:@"cellSegmented"]){
cell.labelFlag.text = @"segmented";
}else{
cell.labelFlag.text = @"switch";
}
return cell;
}
![image](/images/DynamicPrototypesTable/iOS Simulator Screen shot Feb 12, 2014, 5.46.26 PM.png)