標籤:
實現思路:
1.每一條新聞就是一個cell,在cell上添加點贊按鈕。
2.讓cell的控制器成為自訂cell的代理,將點擊了哪一個cell放在代理方法中傳出去。
3.並將這條新聞的ID和目前使用者的ID上傳伺服器。
4.此時要考慮每個使用者只能點擊一次,且當使用者再次點擊的時候,取消點贊(定義一個BOOL變數儲存使用者的點擊次數)。則將上傳的資料取出來,做判斷,如果取出來有資料,說明使用者點擊過,沒有資料那就儲存到網路。再次點擊,將請求下來的資料,即BOOL變數儲存為NO,即取消點贊。
5.到這裡就要考慮取出所有使用者點擊此條新聞的資料怎樣賦值給cell?也就是和取其他資料一起取出,這樣保證每條新聞對應資料量一直,然後一起賦值給cell,這樣就完成了這項功能。
部分重點代碼實現:(bmob)
在cell的代理方法中:
-(void)TableViewForCell:(News_TableViewCell *)cell andbutton:(UIButton *)button{ // 獲得點擊了那一行 NSIndexPath *indexPath = [Mytabview indexPathForCell:cell]; NSString *className = @"dian_zan"; Newmodel *news = _allmodelNews[indexPath.row]; BmobObject *data = [[BmobObject alloc] initWithClassName:className];// 獲得目前使用者 BmobUser *user = [BmobUser getCurrentUser]; BmobQuery *query = [BmobQuery queryWithClassName:className];// 兩個判斷條件,使用者的ID和此條新聞的ID作判斷 [query whereKey:@"newsID" equalTo:news.ID]; [query whereKey:@"userID" equalTo:user.objectId]; [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) { NSLog(@"error == %@",error);// 使用者點擊過 if (array.count!=0) {// 取到已經點贊的那一行 BmobObject *zanObj = array[0];// 使用者取消點贊或者點贊 BOOL zann = [[zanObj objectForKey:@"dianzan"] boolValue]==NO?YES:NO; BmobObject *data = [BmobObject objectWithoutDatatWithClassName:className objectId:zanObj.objectId];// 最終結果上傳雲端 [data setObject:@(zann) forKey:@"dianzan"]; [data updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) { if (!error) { } }]; }else{// 使用者沒有點擊過的話,就儲存使用者點贊 [data setObject:user.objectId forKey:@"userID"]; [data setObject:news.ID forKey:@"newsID"]; [data setObject:@(YES) forKey:@"dianzan"];// [data setObject:@(indexPath.row) forKey:@"row"]; [data saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) { NSLog(@"tijiao == %@",error); if (!error) { } }]; } }]; }
處理網路資料,將網路資料賦值給對應的cell.
1 #pragma mark----------------網路資料下載----------------------- 2 -(NSArray *)allmodelNew{ 3 // 每次調用該方法初始化點贊數組,保證每條新聞的資料互不影響。 4 zan = [NSMutableArray array];//由於資料下載是非同步下載,所以點贊和其他資料各用一個可變資料存放資料 5 NSString *className = @"CampusNews"; 6 NSMutableArray *all = [NSMutableArray array]; 7 BmobQuery *quer = [BmobQuery queryWithClassName:className]; 8 [quer orderByDescending:@"updatedAt"]; 9 quer.limit = 3;10 [quer findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {11 12 for (BmobObject *data in array) {13 // 用一個Newmodel模型來裝載資料14 Newmodel *info = [[Newmodel alloc] init];15 if ([data objectForKey:@"name"]) {16 info.name = [data objectForKey:@"name"];17 }18 if ([data objectForKey:@"text"]) {19 info.text = [data objectForKey:@"text"];20 }21 info.time = [dateformatter stringFromDate:data.updatedAt];22 info.ID = data.objectId;23 #pragma mark---------------點贊計算--------------------------24 // 尋找點贊個數25 BmobQuery *query = [BmobQuery queryWithClassName:@"dian_zan"];26 // 尋找該帳號的所有點贊資料(並且全部是YES的資料)27 [query whereKey:@"newsID" equalTo:data.objectId];28 [query whereKey:@"dianzan" equalTo:@(YES)];29 [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {30 // 可變數組zan31 [zan addObject:@(array.count)];32 33 }];34 35 [all addObject:info];36 }37 _allmodelNews = all;38 if (_allmodelNews.count == _allmodelNews.count) {39 [Mytabview.footer endRefreshing];40 }41 42 [self performSelectorOnMainThread:@selector(upDateUI) withObject:nil waitUntilDone:YES];43 }];44 return _allmodelNews;45 }
IOS開發-項目實戰-點贊功能的實現