TableView 是iOS app 中最罕用的控件,许多代码间接或者间接的关联到table view工作中,包含提供数据、更新tableView、管制tableView行为等等。上面会提供放弃tableView代码整洁和构造清晰的办法。

iOS开发交换技术群:563513413,不论你是大牛还是小白都欢送入驻 ,分享BAT,阿里面试题、面试教训,探讨技术, 大家一起交流学习成长!

UITableViewController vs. UIViewController

TableViewController的个性

table view controllers能够读取table view的数据、设置tabvleView的编辑模式、反馈键盘告诉等等。同时Table view controller可能通过应用UIRefreshControl来反对“下拉刷新”。

Child View Controllers

tableViewController也能够作为child view controller增加到其余的viewController中,而后tableViewController会持续治理tableView,而parentViewController能治理其余咱们关怀的货色。

-(void)addDetailTableView

{

DetailViewController *detail = [DetailViewController new];

[detail setup];

detail.delegate = self;

[self addChildViewController:detail];

[detail setupView];

[self.view addSubview:detail.view];

[detail didMoveToParentViewController:self];

}

如果在应用以上代码时,须要建设child View controller 和 parent view controller之间的分割。比方,如果用户抉择了一个tableView里的cell,parentViewController须要晓得这件事以便可能响应点击工夫。所以最好的办法是table view controller定义一个协定,同时parent view controller实现这个协定。

@protocol DetailViewControllerDelegate

-(void)didSelectCell;

@end

@interface ParentViewController () <DetailViewControllerDelegate>

@end

@implementation ParentViewController

//....

-(void)didSelectCell

{

//do something...

}

@end

尽管这样会导致view controller之间的频繁交换,然而这样保障了代码的低耦合和复用性。

扩散代码

在解决tableView的时候,会有各种各样不同的,逾越model层、controller层、view层的工作。所以很有必要把这些不同的代码扩散开,避免viewController成为解决这些问题的“堆填区”。尽可能的独立这些代码,可能使代码的可读性更好,领有更好的可维护性与测试性。

打消ModelObeject和Cell之间的隔膜

在很多状况下,咱们须要提交咱们想要在view层展现的数据,同时咱们也行维持view层和model层的拆散,所以tableView中的dateSource经常做了超额的工作:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

[cell setup];

NSString *text = self.title;

cell.label.text = text;

UIImage *photo = [UIImage imageWithName:text];

cell.photoView.image = photo;

}

这样dataSorce会变得很芜杂,应该将这些货色分到cell的category中。

@implementation Cell (ConfigText)

-(void)configCellWithTitle:(NSString *)title

{

self.label.text = title;

UIImage *photo = [UIImage imageWithName:title];

cell.photoView.image = photo;

return cell;

}

这样的话dataSource将会变得非常简略。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

[cell configCellWithTitle:self.title];

return cell;

}

复用cell

其实还能够更进一步,让同一个cell变得能够展现多种的modelObject。首先须要在cell中定义一个协定,想要在这个cell中展现的object必须恪守这个协定。而后能够批改分类中config method来让object来恪守这个协定,这样cell就能适应不同的数据类型。

在cell中解决cell状态

如果想要对tableView的行为进行设置,如选中操作后扭转高光状态等,能够在tableViewController中应用委托办法:

-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath

{

Cell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.label.shadowColor = [UIColor greenColor];

}

-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath

{

Cell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.label.shadowColor = nil;

}

然而当想要换出这些cell或者想要从新设计的时候,依然须要适应委托办法。cell外面的detail的实现和委托办法中对detail的实现交错在一起,所以应该将这些逻辑移到cell外面:

@implementation Cell

//...

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

{

[super setHighlighted:highlighted animated:animated];

if(highlighted)

{

self.label.shadowColor = [UIColor greenColor];

}

else

{

self.label.shadowColor = nil;

}

}

@end

一个委托须要晓得一个view的不同状态,但它不须要晓得怎么去批改view或者有哪些属性须要设置来使这个view转变状态,所有的逻辑应该又view来实现,而在内部只是仅仅提供一个API。这样才是view层和controller层实现代码之间的无效拆散。

解决不同的cell类型

如果在一个tableView中有不同的cell类型,dataSource将会变得收缩而难以操作,在上面的代码中,有两个不同的cell类型,一个负责展现图片和题目,另一个负责展现星标。为了拆散解决不同的cell的代码,dataSource办法只是仅仅执行不同cell本人的设置办法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

BOOL isStarRank = self.keys[(NSUInteger)indexPath.row];

UITableViewCell *cell;

if(isStarRank)

{

cell = [self setupStarCell];

}

else

{

cell = [self setupDefaultCell];

}

}

-(StarCell *)setupStarCell

{

//do something...

}

-(UITableViewCell *)setupDefaultCell

{

//do something...

}

编辑TableView

TableView提供了不便的编辑性能,可能删除和挪动cell。这些事件中,tableView的dataSource通过委托办法获取告诉,因而常常在这些委托办法中呈现对数据的批改,而批改数据很显著是model层的工作。model应该提供删除、排序等的接口,这样就可能通过dataSource的办法来调用。从而controller表演了view和model之间的协调者,而不须要晓得model层的实现细节。同时这样model的逻辑会变得更容易测试,因为没有混淆viewController的工作。

总结

tableViewController以及其余controller应该表演model和view的协调者和中介者,而不应该关怀属于view或者model层的工作。谨记这点,让委托和dataSource变得更小和只蕴含公式化的代码。

这不只是缩小tableViewController的体积和复杂度,同时也把域逻辑和界面逻辑放到相干的类中,把实现细节包裹在简略的API接口中,最终进步了代码的可读性和代码的协调能力。