iOS simple drop-down refresh

Source: Internet
Author: User

A small demmo that automatically refreshes the current time on Tableviewcell after a drop-down.

Create a new Uitableviewcontroller, initialize the variable, time, and Uirefreshcontrol in the Viewdidload, with the following code:

#import "TableViewController.h"
@interface Viewcontroller ()
@property (Nonatomic,strong) Nsmutablearray *logs;
@end
@implementation Viewcontroller
-(void) viewdidload{
[Super Viewdidload];
Self.logs=[nsmutablearray Alloc]init];
NSDate *date=[nsdate Alloc]init];
[Self.logs Addobject:date];
Initialize Uirefreshcontrol
Uirefreshcontrol *rc=[uirefreshcontrol Alloc]init];
The caption of the drop-down CONTROL
Rc.attributedtitle=[[nsattributedstring alloc]initwithstring:@ "drop-down refresh"];
Add Event
[RC addtarget:self Action: @selector (Refreshtableview) forcontrolevents:uicontroleventvaluechanged];
SELF.REFRESHCONTROL=RC;
}

The Refreshtableview method code is as follows:

-(void) refreshtableview{
if (self.refreshControl.refreshing) {
Self.refreshcontrol.attributedtitle=[[nsattributedstring alloc]initwithstring:@ "drop-down refresh"];
Add new Simulation Data
NSDate *date=[[nsdate Alloc]init];
[Self.logs Addobject:date];
[Self.refreshcontrol endrefreshing];
[Self.tableview Reloaddata];
}

  

Set Tableviewcell:

-(Nsinteger) Numberofsectionintableview: (uitableview*) tableview{
return 1;
}
-(Nsinteger) TableView: (uitableview*) TableView numberofrowsinsection: (Nsinteger) section{
return [self.logs Count];
}
-(uitableviewcell*) TableView: (uitableview*) TableView Cellforrowsindexpath: (nsindexpath*) indexpath{
Static nsstring *[email protected] "Cell";
UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:cellidentifier];
if (Cell==nil) {
Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellsstylevalue1 Reuseidentifier:cellidentifier];
}
Nsdateformatter*dateformat=[[nsdateformatter Alloc]init];
[Self.dateformat setdateformat:@ "Yyyy-mmm-dd HH:mm:ss:zzz"];
Cell.tablelable.text=[dateformat stringfromdate:[self.logs Objectatindex:[indexpath Row]];
Cell.accessorytype=uitableviewcellaccessorydisclosureindicator;
return cell;
}

The final effect is as follows:

iOS simple drop-down refresh

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.