How To Apply Refresh Control To UITableView - ios

Is there a way to apply a refresh control to a UITableView which is set in a UIViewController?
The solution I found requires subclassing of UITableViewController, which is troublesome for me because I need to subclass a UIViewController not a UITableViewController, and I don't see any method under the documentation of UITableView that allows me to set a refresh control on it.
Any help would be very much appreciated!

use this UIRefreshControl control :
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh1:) forControlEvents:UIControlEventValueChanged];
[Delivered_TBL addSubview:refreshControl];
- (void)refresh1:(UIRefreshControl *)refreshControl
{
[self Service_Call];
[refreshControl endRefreshing];
}

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.backgroundColor = [UIColor clearColor];
refreshControl.tintColor = [UIColor whiteColor];
[refreshControl addTarget:self
action:#selector(getLatestData)
forControlEvents:UIControlEventValueChanged];
[yourTableviewName addSubview:refreshControl];
-(void)getLatestData
{
// here add your reload method
[self XXXXX];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *title = [NSString stringWithFormat:#"Last update: %#", [formatter stringFromDate:[NSDate date]]];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:NSForegroundColorAttributeName];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
refreshControl.attributedTitle = attributedTitle;
[refreshControl endRefreshing];
}

Related

when i am using 3 view on viewcontroller it get slow in objective c

I am using 3 view on one UIViewController. Because of this I have lots of code in viewDidLoad() Like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
customActivityIndicator.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"tmp-0.gif"],[UIImage imageNamed:#"tmp-1.gif"],[UIImage imageNamed:#"tmp-2.gif"],[UIImage imageNamed:#"tmp-3.gif"],[UIImage imageNamed:#"tmp-4.gif"],[UIImage imageNamed:#"tmp-5.gif"],[UIImage imageNamed:#"tmp-6.gif"],[UIImage imageNamed:#"tmp-7.gif"],[UIImage imageNamed:#"tmp-8.gif"],[UIImage imageNamed:#"tmp-9.gif"],[UIImage imageNamed:#"tmp-10.gif"],[UIImage imageNamed:#"tmp-11.gif"],[UIImage imageNamed:#"tmp-12.gif"],[UIImage imageNamed:#"tmp-13.gif"],[UIImage imageNamed:#"tmp-14.gif"],[UIImage imageNamed:#"tmp-15.gif"],nil];
customActivityIndicator.animationDuration = 1.0; // in seconds
customActivityIndicator.animationRepeatCount = 0; // sets to loop
[customActivityIndicator startAnimating];
btn.hidden=YES;
UILabel *lab =[[UILabel alloc] init];
lab.text = [NSString awesomeIcon:FaMailReply];
UIImage *listImage2 = [UIImage imageNamed:#"backicon.png.png"];
UIButton *listButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
listButton2.backgroundColor=[UIColor whiteColor];
[[listButton2 layer] setBorderWidth:0.5f];
listButton2.layer.borderColor =[[UIColor blackColor] CGColor];
listButton2.layer.cornerRadius = btn.bounds.size.width / 3.4;// this value vary as per your desire
listButton2.clipsToBounds = YES;
UIFont *font = [UIFont fontWithName:#"FontAwesome" size:15.0];
UIColor *color = [UIColor blueColor];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
// [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:lab.text attributes:attrsDictionary];
// get the image size and apply it to the button frame
CGRect listButton2Frame = listButton2.frame;
listButton2Frame.size = listImage2.size;
listButton2.frame = listButton2Frame;
[listButton2 setAttributedTitle:attributedStr forState:UIControlStateNormal];
[listButton2 addTarget:self
action:#selector(LogoutClick:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton2 =
[[UIBarButtonItem alloc] initWithCustomView:listButton2];
//Notificaation Icon Button...
UILabel *lablnotification =[[UILabel alloc] init];
// lab.font = [UIFont fontWithName:#"FontAwesome" size:8];
// lab.textColor = [UIColor whiteColor];
lablnotification.text = [NSString awesomeIcon:FaHome];
UIImage *listImage4 = [UIImage imageNamed:#"notification.png"];
UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom];
listButton4.backgroundColor=[UIColor whiteColor];
[[listButton4 layer] setBorderWidth:0.5f];
listButton4.layer.borderColor =[[UIColor blackColor] CGColor];
listButton4.layer.cornerRadius = btn.bounds.size.width / 3.4;// this value vary as per your desire
listButton4.clipsToBounds = YES;
UIFont *fontnotification = [UIFont fontWithName:#"FontAwesome" size:18.0];
UIColor *colornotification = [UIColor blueColor];
NSDictionary *attrsDictionarynotification = [NSDictionary dictionaryWithObjectsAndKeys:fontnotification,NSFontAttributeName,colornotification,NSForegroundColorAttributeName, nil];
// [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
NSAttributedString *attributedStrnotification = [[NSAttributedString alloc] initWithString:lablnotification.text attributes:attrsDictionarynotification];
// get the image size and apply it to the button frame
CGRect listButton4Frame = listButton4.frame;
listButton4Frame.size = listImage4.size;
listButton4.frame = listButton4Frame;
[listButton4 setAttributedTitle:attributedStrnotification forState:UIControlStateNormal];
[listButton4 addTarget:self
action:#selector(ActualNotificationClick:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton4 =
[[UIBarButtonItem alloc] initWithCustomView:listButton4];
UIImage *listImage3 = [UIImage imageNamed:#"ec2.png"];
UIButton *listButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
// get the image size and apply it to the button frame
CGRect listButton3Frame = listButton3.frame;
listButton3Frame.size = listImage3.size;
listButton3.frame = listButton3Frame;
[listButton3 setImage:listImage3 forState:UIControlStateNormal];
[listButton3 addTarget:self
action:#selector(EmployeeClick:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton3 =
[[UIBarButtonItem alloc] initWithCustomView:listButton3];
self.navigationItem.rightBarButtonItems= [NSArray arrayWithObjects:jobsButton2,jobsButton4, nil];
self.navigationItem.leftBarButtonItems=[NSArray arrayWithObjects:jobsButton3, nil];
//NSUserDefault...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
userid = [defaults objectForKey:#"UserId"];
NSLog(#"User Id is =%#",userid);
ServerString=[defaults objectForKey:#"ServerString"];
DefaultRegionIDString=[defaults objectForKey:#"DefaultRegionIDString"];
DefaultBranchIDString=[defaults objectForKey:#"DefaultBranchIDString"];
DefaultSiteIDString=[defaults objectForKey:#"DefaultSiteIDString"];
DefaultLocationString=[defaults objectForKey:#"DefaultLocationString"];
DefaultDateString =[defaults objectForKey:#"DefaultDateString"];
DefaultTimeString =[defaults objectForKey:#"DefaultTimeString"];
DefaultEmployeeNameString=[defaults objectForKey:#"DefaultEmployeeNameString"];
DefaultRegionNameString=[defaults objectForKey:#"DefaultRegionNameString"];
DefaultBranchNameString=[defaults objectForKey:#"DefaultBranchNameString"];
DefaultSiteNameString=[defaults objectForKey:#"DefaultSiteNameString"];
DefaultEventTypeString=[defaults objectForKey:#"DefaultEventTypeString"];
DefaultIncidentTypeString=[defaults objectForKey:#"DefaultIncidentTypeString"];
DefaultsIncidentNameString=[defaults objectForKey:#"DefaultsIncidentNameString"];
SegmentStringCheck=[defaults objectForKey:#"SegmentStringCheck"];
CheckIncidentString=[defaults objectForKey:#"CheckIncidentString"];
if([DefaultEventTypeString isEqualToString:#"1"])
{
lblreportregion.text = DefaultRegionIDString;
lblreportbranch.text = DefaultBranchIDString;
lblreportsite.text = DefaultSiteIDString;
// lblreportinjurytype.text=DefaultIncidentTypeString;
txtreportlocation.text = DefaultLocationString;
txtreportdate.text = DefaultDateString;
txtreporttime.text = DefaultTimeString;
txtreportemp.text = DefaultEmployeeNameString;
txtreportregion.text=DefaultRegionNameString;
txtreportbranch.text=DefaultBranchNameString;
txtreportsite.text=DefaultSiteNameString;
// txtreportinjurytype.text = DefaultsIncidentNameString;
}
else if ([DefaultEventTypeString isEqualToString:#"2"])
{
lblnearregion.text = DefaultRegionIDString;
lblnearbranch.text = DefaultBranchIDString;
lblnearsite.text = DefaultSiteIDString;
txtnearlocation.text = DefaultLocationString;
txtneardate.text = DefaultDateString;
txtneartime.text = DefaultTimeString;
txtnearemp.text = DefaultEmployeeNameString;
txtnearregion.text=DefaultRegionNameString;
txtnearbranch.text=DefaultBranchNameString;
txtnearsite.text=DefaultSiteNameString;
}
else if ([DefaultEventTypeString isEqualToString:#"3"])
{
lblspotingregion.text = DefaultRegionIDString;
lblspotingbranch.text = DefaultBranchIDString;
lblspotingsite.text = DefaultSiteIDString;
txtspotinglocation.text = DefaultLocationString;
txtspotingdate.text = DefaultDateString;
txtspotingtime.text = DefaultTimeString;
txtsportingemp.text = DefaultEmployeeNameString;
txtspotingregion.text=DefaultRegionNameString;
txtspotingbranch.text=DefaultBranchNameString;
txtspotingsite.text=DefaultSiteNameString;
}
if([SegmentStringCheck isEqualToString:#"0"])
{
segment.selectedSegmentIndex = UISegmentedControlNoSegment;
segment.selectedSegmentIndex = 0;
viewspoting.hidden=NO;
viewnear.hidden=YES;
viewreport.hidden=YES;
}
else if ([SegmentStringCheck isEqualToString:#"1"])
{
segment.selectedSegmentIndex = UISegmentedControlNoSegment;
segment.selectedSegmentIndex = 1;
viewspoting.hidden=YES;
viewnear.hidden=NO;
viewreport.hidden=YES;
//Near...
[self nearserverconnection];
[self nearserverconnectionincident];
[self nearserverconnectionactivity];
}
else if ([SegmentStringCheck isEqualToString:#"2"])
{
segment.selectedSegmentIndex = UISegmentedControlNoSegment;
segment.selectedSegmentIndex = 2;
viewspoting.hidden=YES;
viewnear.hidden=YES;
viewreport.hidden=NO;
//Report...
[self reportserverconnection];
[self reportserverconnectioninjury];
}
else
{
segment.selectedSegmentIndex = UISegmentedControlNoSegment;
segment.selectedSegmentIndex = 0;
viewspoting.hidden=NO;
viewnear.hidden=YES;
viewreport.hidden=YES;
}
//.........................//
//Sporting Hide Code...
//Table...
tablesportingbranch.hidden=YES;
tablesportingregion.hidden=YES;
tablesportingsite.hidden=YES;
//Label...
lblsportingactivitytype.hidden=YES;
lblsportingdept.hidden=YES;
lblsportingemp.hidden=YES;
lblspotingbranch.hidden=YES;
lblspotingincidenttype.hidden=YES;
lblspotingregion.hidden=YES;
lblspotingsite.hidden=YES;
//TextFieldDelegate...
txtspotingsite.delegate=self;
txtsportingemp.delegate=self;
txtspotinglocation.delegate=self;
//Date Picker...
pickerspotingdate.hidden=YES;
pickerspotingtime.hidden=YES;
//............................//
//Near Hide Code...
//View...
// viewnear.hidden=YES;
//Table...
tablenearbranch.hidden=YES;
tablenearregion.hidden=YES;
tablenearsite.hidden=YES;
//Label...
lblnearactivitytype.hidden=YES;
lblnearbranch.hidden=YES;
lblneardept.hidden=YES;
lblnearemp.hidden=YES;
lblnearincidenttype.hidden=YES;
lblnearregion.hidden=YES;
lblnearsite.hidden=YES;
//TextFieldDelegate..
txtnearsite.delegate=self;
txtnearemp.delegate=self;
txtnearlocation.delegate=self;
//Date Picker...
pickerneardate.hidden=YES;
pickerneartime.hidden=YES;
//...........................//
//Report Hide View Code...
//View...
// viewreport.hidden=YES;
//Table...
tablereportbranch.hidden=YES;
tablereportinjurytype.hidden=YES;
tablereportregion.hidden=YES;
tablereportsite.hidden=YES;
//Label...
lblreportbranch.hidden=YES;
lblreportdept.hidden=YES;
lblreportemp.hidden=YES;
lblreportinjurytype.hidden=YES;
lblreportregion.hidden=YES;
lblreportsite.hidden=YES;
//TextFieldDelegate...
txtreportsite.delegate=self;
txtreportemp.delegate=self;
txtreportlocation.delegate=self;
//Date Picker...
pickerreporttime.hidden=YES;
pickereportdate.hidden=YES;
//.............................//
//Font Asowme
//Show...
lblspotingShow.font = [UIFont fontWithName:#"FontAwesome" size:15];
lblspotingShow.textColor = [UIColor blackColor];
lblspotingShow.text = [NSString awesomeIcon:FaEye];
//Scroll View...
[scrollspoting setContentSize:CGSizeMake(300, 500)];
[scrollnear setContentSize:CGSizeMake(300, 500)];
[scrollreport setContentSize:CGSizeMake(300, 730)];
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandler:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];
//Button Diamiter...
//Spoting Button...
btnsportinghideadd.layer.cornerRadius = 4; // this value vary as per your desire
btnsportinghideadd.clipsToBounds = YES;
//Near Button...
btnnearhideadd.layer.cornerRadius = 4; // this value vary as per your desire
btnnearhideadd.clipsToBounds = YES;
//Report Button...
btnreportnext.layer.cornerRadius = 4; // this value vary as per your desire
btnreportnext.clipsToBounds = YES;
//Spoting...
[self spotingserverconnection];
[self spotingserverconnectionactivity];
[self spotingserverconnectionincident];
[txtspotingsite addTarget:self action:#selector(spotingtextFieldDidChangeSite:) forControlEvents:UIControlEventEditingChanged];
// prevents the scroll view from swallowing up the touch event of child buttons
[txtnearsite addTarget:self action:#selector(neartextFieldDidChangeSite:) forControlEvents:UIControlEventEditingChanged];
[txtreportsite addTarget:self action:#selector(reporttextFieldDidChangeSite:) forControlEvents:UIControlEventEditingChanged];
UITapGestureRecognizer *tapGesturereportinjury = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideSubViewreportinjury)];
// prevents the scroll view from swallowing up the touch event of child buttons
tapGesturereportinjury.cancelsTouchesInView = NO;
[scrollreport addGestureRecognizer:tapGesturereportinjury];
IssueSelectedIDarray=[[NSMutableArray alloc] init];
IssueSelectedNamearray=[[NSMutableArray alloc] init];
ReportInjurySelectedNamearray=[[NSMutableArray alloc] init];
ReportInjurySelectedIDarray=[[NSMutableArray alloc] init];
[self testInternetConnection];
//Spoting Date and Time..
NSDateFormatter *Spotingdateformatter;
Spotingdateformatter = [[NSDateFormatter alloc] init];
[Spotingdateformatter setDateFormat:#"dd/MM/yyyy"];
SpotingdateString = [Spotingdateformatter stringFromDate:[NSDate date]];
NSLog(#"Current Time =%#",SpotingdateString);
CurrentDate=SpotingdateString;
NSDate * now = [NSDate date];
NSDateFormatter *Spotingtimeformatter = [[NSDateFormatter alloc] init];
[Spotingtimeformatter setDateFormat:#"hh:mm"];
SpotingTimeString = [Spotingtimeformatter stringFromDate:now];
NSLog(#"newDateString %#", SpotingTimeString);
//Near Date and Time..
NSDateFormatter *Neardateformatter;
Neardateformatter = [[NSDateFormatter alloc] init];
[Neardateformatter setDateFormat:#"dd/MM/yyyy"];
NeardateString = [Neardateformatter stringFromDate:[NSDate date]];
NSLog(#"Current Time =%#",NeardateString);
NSDate * nowNear = [NSDate date];
NSDateFormatter *Neartimeformatter = [[NSDateFormatter alloc] init];
[Neartimeformatter setDateFormat:#"hh:mm"];
NearTimeString = [Neartimeformatter stringFromDate:nowNear];
NSLog(#"newDateString %#", NearTimeString);
//Report Date and Time..
NSDateFormatter *Reportdateformatter;
Reportdateformatter = [[NSDateFormatter alloc] init];
[Reportdateformatter setDateFormat:#"dd/MM/yyyy"];
ReportdateString = [Reportdateformatter stringFromDate:[NSDate date]];
NSLog(#"Current Time =%#",ReportdateString);
NSDate * nowReport = [NSDate date];
NSDateFormatter *Reporttimeformatter = [[NSDateFormatter alloc] init];
[Reporttimeformatter setDateFormat:#"hh:mm"];
ReportTimeString = [Reporttimeformatter stringFromDate:nowReport];
NSLog(#"newDateString %#", ReportTimeString);
}
Its working to much slow. Is there is any way to speed it up?
ViewName *Incedent =[[ViewName alloc] initWithNibName:#"ViewName" bundle:nil];
[self.navigationController pushViewController:Incedent animated:YES];
I have used this code to pushviewcontroller.
You need to evaluate what your code is doing, and find out where it is slow. I don't know what this method is doing, but I'm betting the line:
[self testInternetConnection];
is one of the problems. Comment out that line, and see what happens.

adding subview to chat bubble (cell) in iOS

I am working on a chat application where i am using alex barinov's chat bubble example. I am able to send/receive messages. I want to add some labels and views on each chat bubble like time, date, delivered image, etc.
I tried to add some labels over the chat bubble's contentView in UIBubbleTableViewCell.m class in method named -(void)setupInternalData, but the label (date label) gets repeated for every bubble , and it overwrites the previous label, and it contains both the labels.
Here is the address from where i downloaded the project -https://github.com/AlexBarinov/UIBubbleTableView
Below is my code for adding labels to chat bubble view -
-(void)setupInternalData{
_fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, self.frame.size.height-28, 100, 14)];
_fromLabel.numberOfLines = 1;
_fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
_fromLabel.clipsToBounds = YES;
_fromLabel.layer.cornerRadius=5;
[_fromLabel setFont:[UIFont systemFontOfSize:5]];
[self.customView addSubview:_fromLabel];
//display msg sent time
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
_lblMsgTime.text=currentTime;
_lblMsgTime.frame=CGRectMake(12,0, 85, 14);
_lblMsgTime.text=[NSString stringWithFormat:#"%#",currentTime];
_lblMsgTime.textAlignment = NSTextAlignmentRight;
_lblMsgTime.textColor = [UIColor blackColor];
[_fromLabel addSubview:_lblMsgTime];
[_lblMsgTime setAdjustsFontSizeToFitWidth:YES];
//date formater
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"d.M.yyyy";
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *string = [formatter stringFromDate:date];
_lblMsgdate.text=string;
_lblMsgdate.frame=CGRectMake(60 ,0, 85, 14);
_lblMsgdate.text=[NSString stringWithFormat:#"%#",string];
_lblMsgdate.textAlignment = NSTextAlignmentRight;
_lblMsgdate.textColor = [UIColor blackColor];
[_fromLabel addSubview:_lblMsgdate];
[_lblMsgdate setAdjustsFontSizeToFitWidth:YES];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ic__typ__txt.png"]];
backgroundView.frame=CGRectMake(self.frame.origin.x+1, self.frame.size.height-24, 10, 10);
[self.customView addSubview:backgroundView];
[self.customView bringSubviewToFront:_fromLabel];
[self.customView bringSubviewToFront:backgroundView];
[self.contentView addSubview:self.customView];
}
Can anyone please tell me what wrong am i doing here, and what should i be doing. Any help is appreciated. Waiting for your valuable answers.
hey i think you have to create a view and pass it.
create a view in NSBubbleData file's
- (id)initWithText:(NSString *)text date:(NSDate *)date type:(NSBubbleType)type
{
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize size = [(text ? text : #"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = (text ? text : #"");
label.font = font;
label.backgroundColor = [UIColor clearColor];
#if !__has_feature(objc_arc)
[label autorelease];
#endif
UIEdgeInsets insets = (type == BubbleTypeMine ? textInsetsMine : textInsetsSomeone);
return [self initWithView:label date:date type:type insets:insets];
}
in this you have to create a view, in this view add uilable,uiimage which you want replace UILabel *label with custom view.

Pull To Refresh crashes app if I try to scroll while refreshing

I have implemented Pull to Refresh on my app but it crashes when I try to scroll while it is still refreshing. here is the code I'm using:
FYI, I'm calling webServices while refreshing.
In ViewDidLoad:
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self action:#selector(callWebService:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
[self callWebService:refresh];
Method for refresh:
-(void)callWebService:(UIRefreshControl *)refresh
{
static BOOL refreshInProgress = NO;
MessageWebServices *messageWebService = [[MessageWebServices alloc]init];
MessageBO *tempBO = [[MessageBO alloc]init];
if(self.flag)
{
self.navigationItem.title = #"My Posts";
tempBO.projectId = [AppConstants getProjectId];
tempBO.customerId =[AppConstants getCustomerId];
}
else
{
tempBO.projectId = [AppConstants getProjectId];
}
self.messageStore = [[NSMutableArray alloc] init];
if (!refreshInProgress)
{
refreshInProgress = YES;
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.messageStore = [messageWebService getMessageList:tempBO];
dispatch_async(dispatch_get_main_queue(), ^{
[refresh beginRefreshing];
[self.tableView reloadData];
[refresh endRefreshing];
refreshInProgress = NO;
});
});
}
}
Please Help!
I have never used beginRefreshing method. You can easily add refresh control to your tableView.
#pragma mark Refresh Method
-(void)startRefreshing {
if (!self.refreshControl) {
// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.backgroundColor = CRREFRESHBGCOLOR;
self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self action:#selector(callWebservice) forControlEvents:UIControlEventValueChanged];
[yourTable addSubview:self.refreshControl];//add refresh control to your table
}
}
-(void)endRefreshing {
if (self.refreshControl) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *title = [NSString stringWithFormat:#"Last update: %#", [formatter stringFromDate:[NSDate date]]];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:NSForegroundColorAttributeName];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
self.refreshControl.attributedTitle = attributedTitle;
[self.refreshControl endRefreshing];
}
}
After calling/finishing web-service method call [self endRefreshing]; method and reload your table [yourTable reloadData].

ios7: Pull to refresh does not show up on simulator

I am pretty new to iOS programming and for a class project I was trying to implement "pull to refresh feature"
I tried from this link, and implemented same in my MoviesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.moviesTableView.delegate = self;
self.moviesTableView.dataSource = self;
// pull to refresh
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self action:#selector(refreshView:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
// progress bar
MBProgressHUD *HUD = [self getLoadingController];
[HUD showWhileExecuting:#selector(generateMovies) onTarget:self withObject:nil animated:YES];
self.moviesTableView.rowHeight = 100;
[self.moviesTableView registerNib:[UINib nibWithNibName:#"MovieViewCell" bundle:nil] forCellReuseIdentifier:#"MovieViewCell"];
}
and
-(void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing data..."];
// custom logic: get data from server
NSLog(#"refreshing view");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:#"Last updated on %#",[formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refresh endRefreshing];
}
But when I run this in simulator, I do not see it while pulling down the movies table.
What could be the issue here?
I think you're missing adding the HUD into the refresh control. Depending on your view structure, you'd need something like:
[self.refreshLoadingView addSubview:self.hud];
We'd need to see the rest of your code to debug the problem, but here's a tutorial I wrote explaining custom pull-to-refresh controls for iOS (including Swift): http://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

SVPullToRefresh not triggering the action handler when pushed

The first trigger goes ok, but after that, if I try to pull it down, it always shows its still loading.
here is my simple code example on my UITableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
__weak StoresViewController * bSelf = self;
[self.tableView addPullToRefreshWithActionHandler:^{
[bSelf callStoresService];
}];
[self.tableView triggerPullToRefresh];
}
- (void)callStoresService {
[self.tableView.pullToRefreshView stopAnimating];
}
Adding a breakpoint to [self.tableView.pullToRefreshView stopAnimating]; I can see it never gets called again...
I have an old project with almost the same code and its working... anything I am missing here?
Since I am only developing for iOS 6, I decided to use the native refresh:
on my viewDidLoad:
// managing pullToRefresh
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull down to refresh"];
[refresh addTarget:self action:#selector(refreshView:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
[self refreshView:refresh];
[self.refreshControl beginRefreshing];
Then create a refreshView method:
-(void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing..."];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM HH:mm:ss"];
// do what you need
NSString *lastUpdated = [NSString stringWithFormat:#"Last update: %#", [formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
}
dont forget to call [refresh endRefreshing]; when your are done

Resources