iOS Development-project combat-the implementation of the likes feature

Source: Internet
Author: User

Implementation ideas:

1. Each piece of news is a cell, add the Like button on the cell.

2. Let the cell's controller become the proxy for the custom cell, and click on which cell will be sent out in the proxy method.

3. and upload the ID of this news and the current user's ID to the server.

4. Consider that each user can only click once , and when the user clicks again, cancel the likes (define a bool variable to store the user's clicks). Then the uploaded data out, make judgments, if taken out to have data, indicating that the user clicked, no data then stored to the network. Click again to save the requested data, that is, the bool variable as no, that is, cancel the likes.

5. It is necessary to consider how to assign the data to the cell when all users click on the news. That is, with the other data taken out, so that each news corresponding to the amount of data, and then assigned to the cell, this completes this function.

Some key code implementations: (BMOB)

In the proxy method of the cell:

-(void) Tableviewforcell: (News_tableviewcell *) cell Andbutton: (UIButton *) button{//get a click on that lineNsindexpath *indexpath =[Mytabview Indexpathforcell:cell]; NSString*classname =@"Dian_zan"; Newmodel*news =_allmodelnews[indexpath.row]; Bmobobject*data =[[Bmobobject alloc] initwithclassname:classname];//Get Current UserBmobuser *user =[Bmobuser Getcurrentuser]; Bmobquery*query =[Bmobquery querywithclassname:classname];//Two judging criteria, User ID and ID of this news[Query Wherekey:@"NewsID"EqualTo:news.ID]; [Query Wherekey:@"UserID"EqualTo:user.objectId]; [Query Findobjectsinbackgroundwithblock:^ (Nsarray *array, Nserror *error) {NSLog (@"error = =%@", error);//user has clicked        if(array.count!=0) {//take the line that I already liked.Bmobobject *zanobj = array[0];//user cancel likes or likesBOOL Zann = [[Zanobj objectforkey:@"Dianzan"] Boolvalue]==no?Yes:no; Bmobobject*data =[Bmobobject objectwithoutdatatwithclassname:classname objectId:zanObj.objectId];//end result upload to cloud[Data setobject:@ (Zann) Forkey:@"Dianzan"]; [Data updateinbackgroundwithresultblock:^ (BOOL issuccessful, Nserror *error) {                if(!error)                    {                           }            }]; }Else{//If the user has not clicked, save the user likes[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)        {                         }            }];    }              }]; }

The network data is processed and the network data is assigned to the corresponding cell.

1 #pragmaMark----------------Web data Download-----------------------2-(Nsarray *) allmodelnew{3 //each time the method is called to initialize the point like array to ensure that the data for each piece of news does not affect each other. 4Zan = [Nsmutablearray array];//because data download is asynchronous download, point like and other data each with a variable data storage data5NSString *classname =@"campusnews";6Nsmutablearray *all =[Nsmutablearray array];7Bmobquery *quer =[Bmobquery querywithclassname:classname];8[Quer orderbydescending:@"Updatedat"];9Quer.limit =3;Ten[Quer findobjectsinbackgroundwithblock:^ (Nsarray *array, Nserror *error) { One          A          for(Bmobobject *datainchArray) { - //use a Newmodel model to load data -Newmodel *info =[[Newmodel alloc] init]; the             if([Data Objectforkey:@"name"]) { -Info.name = [Data objectforkey:@"name"]; -             } -             if([Data Objectforkey:@"text"]) { +Info.text = [Data objectforkey:@"text"]; -             } +Info.time =[Dateformatter stringFromDate:data.updatedAt]; AInfo.id =Data.objectid; at  #pragmaMark---------------Praise Calculation-------------------------- - //find the number of likes -Bmobquery *query = [Bmobquery querywithclassname:@"Dian_zan"]; - //Find all points like data for this account (and all of the data is Yes) -[Query Wherekey:@"NewsID"EqualTo:data.objectId]; -[Query Wherekey:@"Dianzan"equalto:@ (YES)]; in[Query findobjectsinbackgroundwithblock:^ (Nsarray *array, Nserror *error) { - //variable Array Zan to [Zan addobject:@ (Array.count)]; +          -             }]; the              * [All Addobject:info]; $         }Panax Notoginseng_allmodelnews =All ; -         if(_allmodelnews.count = =_allmodelnews.count) { the [Mytabview.footer endrefreshing]; +         } A  the [Self performselectoronmainthread: @selector (UpDateUI) Withobject:nil Waituntildone:yes]; +            }]; -        return_allmodelnews; $}

iOS Development-project combat-the implementation of the likes feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.