級聯介面(推薦介面)搭建原理,介面搭建

來源:互聯網
上載者:User

級聯介面(推薦介面)搭建原理,介面搭建

 

 


一.整體布局

1.項目需求     點擊左邊cell,右邊的cell資料更新 2.介面搭建     2.1交給兩個控制器管理比較麻煩,點擊一個控制器需要通知另外一個控制器     2. 2因此交給一個控制器管理比較好     2.3用xib搭建,左右各放一個tableView就可以了 3.開發順序     先做左邊的tableView,再做右邊的,因為右邊的資料是根據左邊變化的 二.左邊tableView介面搭建1.自訂cell     左邊一個指標歐一個view   中間位置用label 2.設定資料來源     兩個tableView設定同一個控制器為資料來源和代理,實現方法的時候要先判斷tableView的類型 3.請求資料,查看介面文檔 4.字典轉模型 5.顯示資料 6.運行發現一個tableView頂部被擋住,另一個沒被擋住,為什麼?     蘋果預設只給介面上一個scrollView設定額外捲動區域     只需要取消自動化佈建的額外捲動區域,自己手動設定就可以了 7.選中cell,讓cell的指標顯示     7.1 怎麼實現?          監聽cell選中,選中就讓指標顯示      7.2 但是還要監聽取消選中,把指標隱藏      7.3 怎麼同時監聽一個cell被選中,另一個cell取消選中?          cell自己有一個方法可以同時監聽
1 // 調用時刻:當一個cell選中的時候就會調用,並且一個cell取消選中的時候也會調用2 - (void)setSelected:(BOOL)selected animated:(BOOL)animated 

 

      7.4 cell不需要選中狀態 
1     self.selectionStyle = UITableViewCellSelectionStyleNone;

 

 8.點擊左邊cell的時候,請求右邊tableView的資料     監聽左邊cell的點擊,然後發送網路請求,請求右邊的資料 三.右邊tableView介面搭建1.xib複用     xib也能複用,當兩個介面的xib一樣時,可以用同一個xib,只要給xib傳遞不同的模型即可 2.右邊tableView商務邏輯 3.點擊左邊的cell,發送網路請求,請求右邊的資料 4.請求資料,查看介面文檔     4.1發現有一個參數category_id 需要根據左邊伺服器返回的id 來載入右邊的資料,所以,我們在左邊tableView的模型中要再加一個id屬性     4.2複用模型,模型也能複用,只需要在原來模型中添加需要資料的屬性即可 5.展示資料,點擊左邊cell,右邊就顯示對應的資料 四.整體資料最佳化1.預設選中左邊的第0個cell
1 - (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
 2.預設選中第0個cell.寫在哪裡?     2.1寫在viewDidLoad裡面?          不可以,這個時候還沒有資料      2.2要寫在資料載入成功,而且重新整理表格之後重新整理代碼:
1   [self.categoryTableView reloadData]; 2     // 預設選中第0個cell3    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];4    [self.categoryTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

 

 3.手動選中左邊第0個cell,發現右邊資料沒重新整理     3.1為什麼?          因為 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法必須使用者手動點擊cell才會觸發      3.2怎麼解決?          自己去調用這個方法,寫在預設選中第0個cell後邊就可以了
1  [self tableView:self.categoryTableView didSelectRowAtIndexPath:indexPath];
 4.資料最佳化     4.1每次點擊左邊cell,右邊cell都需要發送請求獲得資料,消耗效能      4.2如果載入過一次,就儲存起來,下次就不用再載入了。          4.3儲存到哪裡?          儲存到對應的分類模型的使用者數組裡面          在分類tableView的模型中定義一個使用者數組,儲存左邊cell對應的右邊的tableView的資料      4.4 怎麼拿到左邊cell對應的一組模型?          請求右邊資料的時候,左邊對應的cell一定是被選中的,通過記錄選中cell對應的模型,就能拿到這個模型      4.5 在選中左側cell的方法中,先判斷模型中user數組(右邊對應的資料數組)是否有值          如果有值,直接重新整理表格,然後return,就不在發送網路請求          如果沒有,就發送網路請求,請求成功後,儲存資料,重新整理表格,展示資料 五.上下拉重新整理1.項目需求     右邊的tableView需要上下拉重新整理功能 2.怎麼實現上下拉重新整理?     使用 MJRefresh架構 3.下拉重新整理,直接載入最新資料,覆蓋掉原來的就可以了     我們原本就是直接用模型中的數組,覆蓋掉原來的資料,所以就不用做移除原來資料的處理了 4.上拉重新整理商務邏輯     4.1上拉重新整理,需要載入更多的資料,怎麼載入更多的資料?          需要一個參數(page 或 ID),來獲得更多的資料          4.2這個參數(page)伺服器會返回,我們需要記錄一下,記錄到哪裡?          應該記錄到左邊tableView的模型中,請求更多資料的時候,從模型中取出這個參數發送請求      4.3 下拉重新整理的時候,要對page參數還原          把page重設為1,否則下拉重新整理,會載入其它頁碼的資料,到值資料錯亂          4.4 載入更多資料成功的時候,我們就要對page +1,因為記錄的page  會作為下次請求參數傳遞          注意:只要請求資料,請求成功的時候,就要對page + 1      4.5 上拉重新整理,對資料的處理          上拉重新整理,需要把原來的資料和新載入的資料一起顯示      4.6 怎麼一起顯示?          用數組儲存載入的更多資料,把這個數組中的元素添加到原來資料數組中      4.7,怎麼把一個數組中的元素,添加到另一個數組中?          通過- (void)addObject:(ObjectType)anObject;方法?          不可以,這個方法會把整個數組作為一個元素,添加到另一個數組中      [_selectCategoryItem.users addObject:users];      4.8.那用哪個方法?
1    - (void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray;
     這個方法會把數組中的每一個元素取出來,添加到另一個數組中 5.上拉重新整理細節處理     5.1 當沒有更多資料的時候,需要隱藏上拉重新整理控制項          5.2 怎麼隱藏?          拿到控制項設定hidden屬性  self.userTableView.mj_footer.hidden      5.3隱藏的條件是什麼?          需要判斷目前使用者組,有沒有更多使用者      5.4 怎麼判斷?          伺服器返回的資料有一個 total_page屬性,如果當前頁>= total_page就沒有更多資料      5.5需要儲存 total_page屬性,儲存到哪裡?          儲存到左邊tableView的模型中,每次請求成功,就把 total_page屬性儲存到對應的使用者組中      5.6 在重新整理表格的時候,當前的page屬性是  當前頁數+ 1 的值          所以設定上拉重新整理隱藏的條件應該是 : page > total_page      5.7 隱藏代碼寫在哪裡?          寫在重新整理表格之後,MJ重新整理架構每次重新整理完資料,會自動判斷是否隱藏,一定要在重新整理方法後設定才有用      5.8 每次點擊左邊cell的時候,也要判斷是否隱藏上拉重新整理控制項,為什麼?          有可能資料只有一頁,不判斷的話,就會顯示上拉重新整理控制項,去重新整理的時候,拿不到更多資料 原始碼
  1 - (void)viewDidLoad {  2     [super viewDidLoad];  3    4     self.title = @"推薦關注";  5     self.automaticallyAdjustsScrollViewInsets = NO;  6     _categoryTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);  7     _userTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);  8     // 分類tableView註冊cell  9     [_categoryTableView registerNib:[UINib nibWithNibName:@"XMGCategoryCell" bundle:nil] forCellReuseIdentifier:categoryID]; 10     // 使用者tableView註冊cell 11     [_userTableView registerNib:[UINib nibWithNibName:@"XMGSubTagCell" bundle:nil] forCellReuseIdentifier:userID]; 12     // 請求分類資料 13     [self loadCategoryData]; 14     // 添加上下拉重新整理 15     [self setupRefreshView];   16 } 17 - (void)setupRefreshView 18 { 19     // 下拉重新整理 20     // 當鬆手,並且下拉重新整理完全顯示的時候,就會觸發下拉重新整理 21     MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewUserData)]; 22     header.automaticallyChangeAlpha = YES; 23     self.userTableView.mj_header = header; 24     25     // 上拉重新整理 26     MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreUserData)]; 27     footer.automaticallyHidden = YES; 28     self.userTableView.mj_footer = footer; 29 } 30   31 - (void)loadCategoryData 32 { 33     AFHTTPSessionManager *mgr = [AFHTTPSessionManager xmg_manager]; 34    35     NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 36     parameters[@"a"] = @"category"; 37     parameters[@"c"] = @"subscribe"; 38     39     [mgr GET:XMGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) { 40         NSArray *dictArr = responseObject[@"list"]; 41         42         _categorys = [XMGCategoryItem mj_objectArrayWithKeyValuesArray:dictArr]; 43         44         [self.categoryTableView reloadData]; 45         46         // 預設選中第0個cell 47         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 48         [self.categoryTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 49         50         [self tableView:self.categoryTableView didSelectRowAtIndexPath:indexPath]; 51         52     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {   53     }]; 54 } 55  56 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 57 { 58     if (tableView == _categoryTableView) { // 顯示分類TableView 59         return _categorys.count; 60     } 61     return _selectCategoryItem.users.count; 62 } 63  64 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 65 { 66     if (tableView == _categoryTableView) { // 顯示分類TableView 67         XMGCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryID]; 68         cell.item = _categorys[indexPath.row]; 69         return cell; 70     }  71      XMGSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:userID]; 72      cell.user =  _selectCategoryItem.users[indexPath.row]; 73     return cell; 74 } 75   76 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 77 { 78     if (tableView == _categoryTableView) { 79         return 44; 80     } 81     return 60 + 1; 82 } 83 // 點擊cell就會調用 84 // 必須使用者手動點擊cell才會觸發 85 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 86 { 87     if (tableView == _categoryTableView) { 88         // 記錄選中分類模型 89         _selectCategoryItem = _categorys[indexPath.row]; 90         // 點擊分類cell 91         // 判斷之前有沒有資料 92         if (_selectCategoryItem.users.count) { 93             [self.userTableView reloadData]; 94             self.userTableView.mj_footer.hidden = _selectCategoryItem.page > _selectCategoryItem.total_page;     95             return; 96         } 97         // 請求右邊使用者資料 98         [self loadNewUserData];      99     }   100 }101 102 // 沒有更多資料的時候,隱藏上拉重新整理控制項103 // 判斷當前分類使用者組 有沒有更多使用者組104 // 載入更多使用者資料105 - (void)loadMoreUserData106 {107     [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];108    109     NSMutableDictionary *parameters = [NSMutableDictionary dictionary];110     parameters[@"a"] = @"list";111     parameters[@"c"] = @"subscribe";112     parameters[@"category_id"] = _selectCategoryItem.id;113     parameters[@"page"] = @(_selectCategoryItem.page);114    115     [self.mgr GET:XMGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {116        117         [self.userTableView.mj_footer endRefreshing];118        119         _selectCategoryItem.page++;120         NSArray *dictArr = responseObject[@"list"];121        122         NSArray *users = [XMGUserItem mj_objectArrayWithKeyValuesArray:dictArr];123        124         // 取出數組中所有元素,添加到新數組125 //        [_selectCategoryItem.users addObject:users];126         [_selectCategoryItem.users addObjectsFromArray:users];127        128         [self.userTableView reloadData];129        130         // 控制上拉控制項是否顯示,一定要在reloadData之後131         self.userTableView.mj_footer.hidden = _selectCategoryItem.page > _selectCategoryItem.total_page;132     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {    133     }];134 }135 136 // 載入更新使用者資料137 - (void)loadNewUserData138 {139     _selectCategoryItem.page = 1;140     [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];141    142     NSMutableDictionary *parameters = [NSMutableDictionary dictionary];143     parameters[@"a"] = @"list";144     parameters[@"c"] = @"subscribe";145     parameters[@"category_id"] = _selectCategoryItem.id;146    147     [self.mgr GET:XMGBaseUrl parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {148        149         _selectCategoryItem.page++;150        151         // 記錄當前分類總頁碼數152         _selectCategoryItem.total_page = [responseObject[@"total_page"] integerValue];153        154         // 結束重新整理155         [self.userTableView.mj_header endRefreshing];156        157         NSArray *dictArr = responseObject[@"list"];158        159         _selectCategoryItem.users = [XMGUserItem mj_objectArrayWithKeyValuesArray:dictArr];160        161         [self.userTableView reloadData];162        163         self.userTableView.mj_footer.hidden = _selectCategoryItem.page > _selectCategoryItem.total_page;164   165     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {      166     }];167 }

 喜歡就推薦下吧,哈哈

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.