功能簡介
每一行cell跳轉(zhuǎn)一個不同的頁面。
例如:
解決方法
第一種
?很多人都會用if-else來進(jìn)行判斷,這樣即麻煩又難維護(hù)項目。不推薦使用這種方案,請看第二種解決方案。
第二種(推薦方案)
1):首先創(chuàng)建一個顯示數(shù)據(jù)的模型(HomeItem):包括圖片屬性,標(biāo)題屬性,打算跳轉(zhuǎn)的控制器屬性(用Class修飾)。再寫一個類和對象的構(gòu)造器。
@interface HomeItem : NSObject
@property (nonatomic, copy) NSString *iconIamge;///圖片
@property (nonatomic, copy) NSString *title;///標(biāo)題
@property (nonatomic, assign) Class vc;///打算跳轉(zhuǎn)的控制器
- (instancetype)initWithIconIamge:(NSString *)iconIamge title:(NSString *)title vc:(Class)vc;
+ (instancetype)homeItemWithIconIamge:(NSString *)iconIamge title:(NSString *)title vc:(Class)vc;
2):創(chuàng)建一個存放數(shù)據(jù)(HomeItem)的模型(HomeGroup):包活每個分區(qū)的區(qū)頭內(nèi)容、區(qū)尾內(nèi)容,存放數(shù)據(jù)(HomeItem)的數(shù)組屬性。
@interface HomeGroup : NSObject
@property (nonatomic, copy) NSString *header;///區(qū)頭內(nèi)容
@property (nonatomic, copy) NSString *footer;///區(qū)尾內(nèi)容
@property (nonatomic, strong) NSArray *groups;///存放數(shù)據(jù)
3):往數(shù)據(jù)源中添加數(shù)據(jù):
HomeItem *item1 = [HomeItem homeItemWithIconIamge:@"aio_icons_groupvideo" title:@"看點" vc:[ViewController class]];? //創(chuàng)建HomeItem
HomeItem *item2 = [HomeItem homeItemWithIconIamge:@"aio_icons_location" title:@"京東購物" vc:[ViewController class]]; //創(chuàng)建HomeItem
HomeItem *item3 = [HomeItem homeItemWithIconIamge:@"aio_icons_music" title:@"閱讀" vc:[ViewController class]]; //創(chuàng)建HomeItem
HomeItem *item4 = [HomeItem homeItemWithIconIamge:@"aio_icons_pacamera" title:@"音樂" vc:[ViewController class]]; //創(chuàng)建HomeItem
HomeItem *item5 = [HomeItem homeItemWithIconIamge:@"aio_icons_pic" title:@"NOW直播" vc:[ViewController class]]; //創(chuàng)建HomeItem
HomeItem *item6 = [HomeItem homeItemWithIconIamge:@"aio_icons_ptt" title:@"熱門活動" vc:[ViewController class]]; //創(chuàng)建HomeItem
HomeGroup *group = [[HomeGroup alloc]init]; //創(chuàng)建HomeGroup
group.header = @"dfndknf"; //創(chuàng)建HomeGroup的區(qū)頭內(nèi)容
group.footer = @"dmd"; //創(chuàng)建HomeGroup的區(qū)尾內(nèi)容
group.groups = @[item1, item2, item3, item4, item5, item6]; //創(chuàng)建HomeGroup的數(shù)組內(nèi)容
[_dataSoucre addObject:group]; //把HomeGroup添加到數(shù)據(jù)源中
4):刷新表格(UITableView)
[self.tableView reloadData];
5):點擊cell實現(xiàn)跳轉(zhuǎn)指定的頁面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
HomeItem *item = [_dataSoucre[indexPath.section] groups][indexPath.row];
[self.navigationController pushViewController:[[item.vc alloc]init] animated:YES];
}
希望此篇文章能幫到你們,如有錯誤請指出。
Demo地址: QQ動態(tài)Demo地址