I am implementing ActionSheetPickerView on my program but when I try to select ActionSheetStyleTextPicker there is nothing comes in PickerView but same code was working for ActionSheetStyleDatePicker and i am able to select date.
I am using IQActionSheetPickerView in my program github.com/hackiftekhar/IQActionSheetPickerView
in my tableView I write this code
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell=(CustomCell *)[ProfileTable dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomeCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.section==0) {
if ([[[ProfileTableArray objectAtIndex:indexPath.row] valueForKey:#"DT"]isEqualToString:#"D"]) {
IQActionSheetPickerView *picker = [[IQActionSheetPickerView alloc] initWithTitle:#"Date Picker" delegate:self];
[picker setActionSheetPickerStyle:IQActionSheetPickerStyleDatePicker];
[picker setTag:1];
//[picker show];
[cell.SInfoTxt setInputView:picker];
} else {
IQActionSheetPickerView *picker = [[IQActionSheetPickerView alloc] initWithTitle:#"Single Picker" delegate:self];
[picker setTitlesForComponenets:customDValues];
[picker setTag:2];
[picker setActionSheetPickerStyle:IQActionSheetPickerStyleTextPicker];
//[picker show];
[cell.SInfoTxt setInputView:picker];
}
}
and I have write this for textPicker or DatePicker
-(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectTitles:(NSArray *)titles
{
switch (pickerView.tag)
{
case 1:{
NSString * dateStr =[NSString stringWithFormat:#"%#",[titles objectAtIndex:0]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *toDateTime = [dateFormat dateFromString:dateStr];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:toDateTime];
[(UITextField *)[self.view viewWithTag:tag] setText:stringDate];
[self.view endEditing:YES];
break;
}
case 2: {
//here i want to show my NSArray which was am already stored in array
// so how can i call Textpicker here..??
break;
}
}
Related
In my custom cell there are three labels: name, date and time.
When I search from namearray it shows proper filtered results, but my date and time label values remain unchanged, it always shows the values of my first and second record's date and time detail.
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(isFiltered)
{
return [arrsearchresult count];
}
return [arrfullname count];
}
// tableView delegate methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
AppoinmentTableViewCell *cell = (AppoinmentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AppoinmentTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.btnCall.tag = indexPath.row;
cell.btnMail.tag = indexPath.row;
cell.btnSms.tag = indexPath.row;
cell.btnCreatePlan.tag = indexPath.row;
cell.btnAppointment.tag = indexPath.row;
cell.btnStatus.tag=indexPath.row;
}
cell.clipsToBounds = YES;
if ([strCheckVC isEqualToString:#"OpenLead"])
{
cell.lblFullname.text = [arrfullname objectAtIndex:indexPath.row];
[self databaseOpen];
NSString *query_user=[ NSString stringWithFormat:#"Select * from lead_status where row_lead_id = %# and lead_followup_datetime != ' ' order by rowid desc limit 1",[arrRowid objectAtIndex:indexPath.row]];
NSArray *rowids = [[NSArray alloc]init];
rowids =[[database executeQuery:query_user]mutableCopy];
NSLog(#"%#",rowids);
[database close];
if (rowids.count > 0 )
{
NSString *dateString = [[rowids valueForKey:#"lead_followup_datetime"]objectAtIndex:0];
NSArray *components = [dateString componentsSeparatedByString:#" "];
NSString *date = components[0];
NSString *time = components[1];
// date convert
NSString *dateStr = [NSString stringWithFormat:#"%#",date];
NSString *timestr = [NSString stringWithFormat:#"%#",time];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm:ss"];
NSDate *date11 = [dateFormat dateFromString:timestr];
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"hh:mm a"];
NSString *depResult = [df stringFromDate:date11];
cell.lblTime.text = depResult;
dateStr = [NSString stringWithFormat:#"%#",date];
// Convert string to date object
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-mm-dd"];
NSDate *viewdt = [dateFormat dateFromString:dateStr];
//date11 = [dateFormat dateFromString:dateStr];
NSLog(#"%#",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date22 = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
// df = [[NSDateFormatter alloc]init];
// [df setDateFormat:#"dd MMM yyyy"];
// depResult = [df stringFromDate:viewdt];
//
cell.lblDate.text = dateWithNewFormat;
}
else
{
// date convert
NSString *dateStr = [NSString stringWithFormat:#"%#",[appointmenttime objectAtIndex:indexPath.row]];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"%#",date);
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"hh:mm a"];
NSString *depResult = [df stringFromDate:date];
cell.lblTime.text = depResult;
dateStr = [NSString stringWithFormat:#"%#",[arrappointment_date objectAtIndex:indexPath.row]];
// Convert string to date object
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-mm-dd"];
date = [dateFormat dateFromString:dateStr];
NSLog(#"%#",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date22 = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
// df = [[NSDateFormatter alloc]init];
// [df setDateFormat:#"dd MMM yyyy"];
// depResult = [df stringFromDate:date];
//
cell.lblDate.text = dateWithNewFormat;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([[arremail objectAtIndex:indexPath.row] isEqual:[NSNull null]] || [[arremail objectAtIndex:indexPath.row] isEqualToString:#"<null>"] || [[arremail objectAtIndex:indexPath.row] isEqualToString:#""] )
{
cell.btnMail.hidden = NO;
}
NSUInteger hours_passed = [[arrhours_passed objectAtIndex:indexPath.row] integerValue];
if (hours_passed <= 72)
{
cell.colorView.backgroundColor = [UIColor whiteColor];
}
else if (hours_passed <= 144)
{
cell.colorView.backgroundColor = [UIColor colorWithRed:219.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
cell.lblDate.textColor = [UIColor whiteColor];
cell.lblTime.textColor = [UIColor whiteColor];
cell.lblFullname.textColor = [UIColor whiteColor];
}
else if (hours_passed <= 216)
{
cell.colorView.backgroundColor = [UIColor colorWithRed:202.0/255.0 green:114.0/255.0 blue:114.0/255.0 alpha:1.0];
cell.lblDate.textColor = [UIColor whiteColor];
cell.lblTime.textColor = [UIColor whiteColor];
cell.lblFullname.textColor = [UIColor whiteColor];
}
else if (hours_passed <= 288)
{
cell.colorView.backgroundColor = [UIColor colorWithRed:189.0/255.0 green:78.0/255.0 blue:78.0/255.0 alpha:1.0];
cell.lblDate.textColor = [UIColor whiteColor];
cell.lblTime.textColor = [UIColor whiteColor];
cell.lblFullname.textColor = [UIColor whiteColor];
}
[self databaseOpen];
NSString *str = [NSString stringWithFormat:#"Select * from lead_followup where rowid=%#",[arrRowid objectAtIndex:indexPath.row]];
NSMutableArray *arr = [[NSMutableArray alloc]init];
arr = [[database executeQuery:str]mutableCopy];
NSLog(#"arr data %#",arr);
[database close];
NSString *strsms;
if (arr.count != 0)
{
strsms = [NSString stringWithFormat:#"%#",[[arr valueForKey:#"followed_by_sms"]objectAtIndex:0]];
if ([strsms isEqualToString:#"0"])
{
if (hours_passed <= 72)
{
[cell.btnSms setImage:[UIImage imageNamed:#"Chatgray"] forState:UIControlStateNormal];
}
else if (hours_passed > 72)
{
//white
[cell.btnSms setImage:[UIImage imageNamed:#"chatwhite"] forState:UIControlStateNormal];
}
}
/*else
{
// green icon
[cell.btnSms setImage:[UIImage imageNamed:#"Chatgreen"] forState:UIControlStateNormal];
}*/
NSString *strCall = [NSString stringWithFormat:#"%#",[[arr valueForKey:#"followed_by_call"]objectAtIndex:0]];
NSLog(#"%#",strCall);
if ([strCall isEqualToString:#"0"])
{
if (hours_passed <= 72)
{
//blackCallGray
[cell.btnCall setImage:[UIImage imageNamed:#"CallGray"] forState:UIControlStateNormal];
}
else if (hours_passed > 72)
{
[cell.btnCall setImage:[UIImage imageNamed:#"CallWhite"] forState:UIControlStateNormal];
}
}
else
{
[cell.btnCall setImage:[UIImage imageNamed:#"CallGreen"] forState:UIControlStateNormal];
// green icon
}
NSString *strmail = [NSString stringWithFormat:#"%#",[[arr valueForKey:#"followed_by_mail"]objectAtIndex:0]];
NSLog(#"%#",strmail);
if ([strmail isEqualToString:#"0"])
{
if (hours_passed <= 72)
{
[cell.btnMail setImage:[UIImage imageNamed:#"Mailgray"] forState:UIControlStateNormal];
}
else if (hours_passed > 72)
{
[cell.btnMail setImage:[UIImage imageNamed:#"MailWhite"] forState:UIControlStateNormal];
//white
}
}
/*else
{
[cell.btnMail setImage:[UIImage imageNamed:#"MailGreen"] forState:UIControlStateNormal];
// green icon
}*/
}
[cell.btnCall addTarget:self
action:#selector(callDo:) forControlEvents:UIControlEventTouchDown];
[cell.btnSms addTarget:self
action:#selector(smsDo:) forControlEvents:UIControlEventTouchDown];
[cell.btnMail addTarget:self
action:#selector(mailDo:) forControlEvents:UIControlEventTouchDown];
[cell.btnStatus addTarget:self
action:#selector(Status:) forControlEvents:UIControlEventTouchDown];
[cell.btnAppointment addTarget:self
action:#selector(AddappointmentId:) forControlEvents:UIControlEventTouchDown];
[cell.btnCreatePlan addTarget:self
action:#selector(CreatePlan:) forControlEvents:UIControlEventTouchDown];
if(self.btnSearchByName.selected==YES)
{
if(isFiltered)
{
cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
/*cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];*/
}
}
else
{
if(isFiltered)
{
//cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
//cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];
}
}
}
else
{
cell.lblFullname.text = [arrfullname objectAtIndex:indexPath.row];
cell.btnSms.hidden = YES;
cell.btnCall.hidden = YES;
cell.btnMail.hidden = YES;
cell.colorView.hidden =YES;
// cell.lblTime.text = [appointmenttime objectAtIndex:indexPath.row];
//cell.lblDate.text = [arrappointment_date objectAtIndex:indexPath.row];
NSString *dateStr = [NSString stringWithFormat:#"%#",[appointmenttime objectAtIndex:indexPath.row]];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"%#",date);
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"hh:mm a"];
NSString *depResult = [df stringFromDate:date];
cell.lblTime.text = depResult;
dateStr = [NSString stringWithFormat:#"%#",[arrappointment_date objectAtIndex:indexPath.row]];
// Convert string to date object
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-mm-dd"];
date = [dateFormat dateFromString:dateStr];
NSLog(#"%#",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date22 = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date22];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
// df = [[NSDateFormatter alloc]init];
// [df setDateFormat:#"dd MMM yyyy"];
// depResult = [df stringFromDate:date];
//
cell.lblDate.text = dateWithNewFormat;
}
/* UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor clearColor];
[cell setSelectedBackgroundView:bgColorView];*/
if(self.btnSearchByName.selected==YES)
{
if(isFiltered)
{
cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
/*cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];*/
}
}
else
{
if(isFiltered)
{
//cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];
cell.lblDate.text = [arrsearchresult objectAtIndex:indexPath.row];
//cell.lblTime.text = [arrsearchresult objectAtIndex:indexPath.row];
}
}
return cell;
}
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([strCheckVC isEqualToString:#"OpenLead"])
{
if(self.btnSearchByName.selected==YES)
{
if(searchText.length==0)
{
isFiltered=NO;
}
else
{
isFiltered=YES;
arrsearchresult=[[NSMutableArray alloc]init];
for ( NSString * str in arrfullname)
{
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[arrsearchresult addObject:str];
}
}
}
}
else
{
if(searchText.length==0)
{
isFiltered=NO;
}
else
{
isFiltered=YES;
arrsearchresult=[[NSMutableArray alloc]init];
for ( NSString * str in arrleadCreatedDate)
{
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[arrsearchresult addObject:str];
}
}
}
}
}
else
{
if(self.btnSearchByName.selected==YES)
{
if(searchText.length==0)
{
isFiltered=NO;
}
else
{
isFiltered=YES;
arrsearchresult=[[NSMutableArray alloc]init];
for ( NSString * str in arrfullname)
{
NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[arrsearchresult addObject:str];
}
}
}
}
else
{
if(searchText.length==0)
{
isFiltered=NO;
}
else
{
isFiltered=YES;
arrsearchresult=[[NSMutableArray alloc]init];
NSString *searchString = searchBar.text;
for ( NSString * str in arrdatetime)
{
/*NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound)
{
[arrsearchresult addObject:str];
}*/
NSComparisonResult result = [str compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame) {
[arrsearchresult addObject:str];
}
}
}
}
}
[self.tblAppoinment reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.tblAppoinment resignFirstResponder];
}
In my app, I use Parse. I have it set up like this:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// The className to query on
self.parseClassName = #"Prayers";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 20;
}
return self;
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:#"Prayers"];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByDescending:#"createdAt"];
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
self.theObject = object;
BOOL anony = [object[#"Anonymous"] boolValue];
if (!anony) {
NSString *names = [[object[#"FirstName"] stringByAppendingString:#" "] stringByAppendingString:object[#"LastName"]];
cell.profileName.text = names;
cell.contentLabel.text = object[#"Request"];
cell.firstName = object[#"FirstName"];
cell.lastName = object[#"LastName"];
cell.iostoken = object[#"DeviceID"];
cell.request = names;
cell.prayerObject = object;
PFFile *thumbnail = object[#"ProfilePic"];
cell.profilePic.image = [UIImage imageNamed:#"AppIcon60x60#2x.png"];
/*[cell.commentButton addTarget:self action:#selector(didTapCommentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.commentButton setTitle:#"Share" forState:UIControlStateNormal];
[cell.commentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];*/
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];
cell.profilePic.image = thumbnailImage;
}];
NSString *dates = object[#"dateMade"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM_dd_yyyy"];
NSDate *datefromstring = [formatter dateFromString:dates];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MMM dd, yyyy"];
cell.dateLabel.text = [formatter2 stringFromDate:datefromstring];
UIFont *cellFont = [UIFont fontWithName:#"Verdana-Bold" size:15];
UIFont *cellFont2 = [UIFont fontWithName:#"Verdana-Bold" size:12];
}
else {
// Configure the cell to show todo item with a priority at the bottom
cell.profileName.text = object[#"Title"];
cell.contentLabel.text = object[#"Request"];
cell.firstName = object[#"FirstName"];
cell.lastName = object[#"LastName"];
cell.iostoken = object[#"DeviceID"];
cell.request = object[#"Title"];
cell.prayerObject = object;
PFFile *thumbnail = object[#"ProfilePic"];
cell.profilePic.image = [UIImage imageNamed:#"AppIcon60x60#2x.png"];
[thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];
cell.profilePic.image = thumbnailImage;
}];
NSString *dates = object[#"dateMade"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM_dd_yyyy"];
NSDate *datefromstring = [formatter dateFromString:dates];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MMM dd, yyyy"];
cell.dateLabel.text = [formatter2 stringFromDate:datefromstring];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
PFObject *entry = [self.objects objectAtIndex:indexPath.row];
NSString *commentString = entry[#"Request"];
NSString *nameString = #"";
NSLog(#"%#", commentString);
return [Cell heightForCellWithContentString:(NSString *)commentString] +25 ;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
if (_webViewController2 == nil) {
self.webViewController2 = [[[WebViewController2 alloc] initWithNibName:#"WebViewController2" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController2.entry = entry;
[self.navigationController pushViewController:_webViewController2 animated:YES];
[self.objects objectAtIndex:indexPath.row];
}
else {
PFObject *entry = [self.objects objectAtIndex:indexPath.row];
if (_webViewController == nil) {
self.webViewController = [[[WebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
_webViewController.finalObject = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
#endif
}
As I understand, that should load up 20 items at a time, and when you scroll to the end of those, load up the next 20. However, once I have 20 items, the app crashes, and I get this message:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 20 beyond bounds [0 .. 19]'
After putting an exception breakpoint in, the line causing the error is in the heightForRow method
PFObject *entry = [self.objects objectAtIndex:indexPath.row];
What's going on?
My problem is I have a Custom Cell which contains a label.
The value of label would be fetched via webServices which would be in TIME .Initially if we go by static data i.e. for e.g.: 10:54 PM, then on click of that cell it should set me a reminder of that value set on label.And off course should notify me on the said time.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CC *cell=(CC*)[tableView cellForRowAtIndexPath:indexPath];
NSDate *currentDate=[NSDate date];
NSString *dateStr= cell.timeLabel.text;
UILocalNotification *localNotification =[[UILocalNotification alloc]init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"hh:mm a"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *preFix=[dateFormatter stringFromDate:currentDate];
NSString *datetoFire=[NSString stringWithFormat:#"%# %#",preFix,dateStr];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSLog(#"date is %#",[dateFormatter dateFromString:datetoFire]);
if (localNotification==nil) {
return;
}
localNotification.fireDate=[dateFormatter dateFromString:datetoFire];
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody=#"Good Morning dude..!";
localNotification.repeatInterval=0; //The default value is 0, which means don't repeat.
localNotification.soundName=UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CC *cell = (CC *) [tableView dequeueReusableCellWithIdentifier:#"CC"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CC" owner:self options:nil];
cell = (CC *) [nib objectAtIndex:0];
}
return cell;
}
I tried doing this, but this isn't working.
Thank You.
Take a dynamic array that will contain values populated from webservice and put populated time to cell dynamically in your cell.In cellForRow method.
I have taken static array as given below
time = [[NSMutableArray alloc] initWithObjects:#"5:35 PM",#"4:56 AM",#"6:00 PM",#"7:32 AM",nil];
you have to put values in this array dynamically as u will get populated data from webservice.
and reload table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CC *cell = (CC *) [tableView dequeueReusableCellWithIdentifier:#"CC"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CC" owner:self options:nil];
cell = (CC *) [nib objectAtIndex:0];
}
cell.textLabel.text=[time objectAtIndex:indexPath.row];
return cell;
}
And in did select method assign fire date from array not from cell text label as
NSString *dateStr=[time objectAtIndex:indexPath.row];
I've retrieved data from 2 different shared Google Calendars, and stored the data in an array. I need to sort the data into a sectioned UITableView, sorted by date.
Heres my code:
CalendarModel.h
#import "JSONModel.h"
#import "Time.h"
#interface CalendarModel : JSONModel
#property (strong, nonatomic) NSString* title;
#property (strong, nonatomic) NSArray<Time>* time;
#end
CalendarModel.m
#import "CalendarModel.h"
#implementation CalendarModel
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithDictionary:#{
#"gd$when": #"time",
#"title.$t": #"title",
}];
}
#end
Time.h
#import "JSONModel.h"
#protocol Time #end
#interface Time : JSONModel
#property (strong, nonatomic) NSString* startTime;
#property (strong, nonatomic) NSString* endTime;
#end
Time.m does nothing, as it's handled by JSONModel
SportsViewController.m
#import "SportsViewController.h"
#import "JSONModelLib.h"
#import "CalendarModel.h"
#import "Time.h"
#import "JSONValueTransformer.h"
#interface SportsViewController ()
#property (strong, nonatomic) NSMutableArray *events;
#property (strong, nonatomic) NSArray *music;
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate;
- (NSDate *)dateByAddingYears:(NSInteger)numberOfYears toDate:(NSDate *)inputDate;
#end
#implementation SportsViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//make HTTP call
NSString* searchCall = [NSString stringWithFormat:#"http://www.google.com/calendar/feeds/kao1d80fd2u5kh7268caop11o4%%40group.calendar.google.com/public/full?alt=json"];
NSString* searchCall2 = [NSString stringWithFormat:#"http://www.google.com/calendar/feeds/3qag5m8iad46mtvsnnqbtrcjjg%%40group.calendar.google.com/public/full?alt=json"];
[JSONHTTPClient getJSONFromURLWithString: searchCall
completion:^(NSDictionary *json, JSONModelError *err) {
//got JSON back
NSLog(#"Got Sports JSON from web: %#", json);
if (err) {
[[[UIAlertView alloc] initWithTitle:#"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles: nil] show];
return;
}
//initialize the models
_events = [CalendarModel arrayOfModelsFromDictionaries:
json[#"feed"][#"entry"]
];
if (_events) NSLog(#"Loaded successfully sports models");
//show the Events
[_events addObjectsFromArray:_music];
NSLog(#"%#", _events);
[self.tableView reloadData];
}];
[JSONHTTPClient getJSONFromURLWithString: searchCall2
completion:^(NSDictionary *json2, JSONModelError *err2) {
//got JSON back
NSLog(#"Got Music JSON from web: %#", json2);
if (err2) {
[[[UIAlertView alloc] initWithTitle:#"Error"
message:[err2 localizedDescription]
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles: nil] show];
return;
}
//initialize the models
_music = [CalendarModel arrayOfModelsFromDictionaries:
json2[#"feed"][#"entry"]
];
if (_music) NSLog(#"Loaded successfully music models");
[_events addObjectsFromArray:_music];
//show the Events
[self.tableView reloadData];
}];
//[events addObjectsFromArray:music];
[self.tableView reloadData];
}
;
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - table methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _events.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CalendarModel* event = _events[indexPath.row];
NSString *dato = [[event.time objectAtIndex:0] startTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
NSDate *gmtDate = [formatter dateFromString: dato];
formatter.dateFormat = #"dd-MM-yyyy HH:mm";
dato = [formatter stringFromDate:gmtDate];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SportCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%#",
event.title
];
cell.detailTextLabel.text = dato;
return cell;
}
#end
So basically all the data i need sorted resides in the _events array. I just do not get how i sort it by date into sections. The reason why startTime and endTime are NSStrings is that, that's what i am returned by the call to the shared calendar on Google
First group all your events - you'll have to take care of variable scoping (I'm using days and groupedEvents locally but you'll have to declare those as ivars/properties)
NSMutableArray *days = [NSMutableArray array];
NSMutableDictionary *groupedEvents = [NSMutableDictionary dictionary];
- (void)groupEventsIntoDays
{
for (CalendarModel *event in _events)
{
NSString *dato = [[event.time objectAtIndex:0] startTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
NSDate *gmtDate = [formatter dateFromString: dato];
if (![days containsObject:gmtDate])
{
[days addObject:gmtDate];
[groupedEvents setObject:[NSMutableArray arrayWithObject:event] forKey:gmtDate];
}
else
{
[((NSMutableArray*)[groupedEvents objectForKey:gmtDate]) addObject:event];
}
}
days = [[days sortedArrayUsingSelector:#selector(compare:)] mutableCopy];
}
Section headers:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width, 30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:header.bounds];
[header addSubview:headerLabel];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"dd-MM-yyyy HH:mm";
NSString *dateAsString = [formatter stringFromDate:[days objectAtIndex:section]];
[headerLabel setText:dateAsString];
return header;
}
I only touched the first two lines in your cellForRow - you can modify the rest as needed for display/formatting:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *date = [days objectAtIndex:indexPath.section];
CalendarModel *event = [((NSMutableArray*)[groupedEvents objectForKey:date]) objectAtIndex:indexPath.row];
NSString *dato = [[event.time objectAtIndex:0] startTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
NSDate *gmtDate = [formatter dateFromString: dato];
formatter.dateFormat = #"dd-MM-yyyy HH:mm";
dato = [formatter stringFromDate:gmtDate];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SportCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%#",
event.title
];
cell.detailTextLabel.text = dato;
return cell;
}
My UITableView is not scrolling properly I am using customcell as
My cell for rowAtIndexPath looks like this:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCellWeather *cell = (CustomCellWeather *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:#"CustomCellWeather" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCellWeather *)currentObject;
// cell = [[[CustomCellWeather alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
break;
}
}
}
int index = indexPath.row;
cell.dayDateLabel.textColor = [UIColor colorWithRed:126/255.0 green:212/255.0 blue:252/255.0 alpha:1];
if(index == 0) {
cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:102/255.0 blue:179/255.0 alpha:0.5];
cell.otherCondition.text = #"";
cell.dayTempMin_HumidityLabel.text = #"";
cell.dayConditionLabel.text = #"";
cell.otherWind.text = #"";
cell.dayWindLabel.text = #"";
cell.dayDateLabel.text = #"Current";
cell.dayTempMaxLabel.text = [[[resCurrTempC stringByAppendingString:#"°C/"] stringByAppendingString:resCurrTempF] stringByAppendingString:#"°F"];
cell.todayCondition.text = resCurrWeatherDesc;
cell.currentWind.text = [[[currWindDir stringByAppendingString:#" at "] stringByAppendingString:currWindSpeed] stringByAppendingString:#"mph"];
cell.currentHumidity.text = [resHumidity stringByAppendingString:#"%"];
NSURL *url = [NSURL URLWithString:currIcon];
cell.dayImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}else {
cell.hiddenLabel.text = #""; //represents todayCondition
cell.todayWind.text = #"";
cell.currentWind.text = #"";
cell.todayHumidity.text = #"";
cell.currentHumidity.text = #"";
if(index == 1) {
cell.contentView.backgroundColor = [UIColor colorWithRed:124/255.0 green:123/255.0 blue:123/255.0 alpha:0.5];
cell.dayDateLabel.text = #"Today";
}else{
//NSDate *today = [NSDate date];
NSString *dateString = [responseDate objectAtIndex:index-1];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE, MMM dd"];
NSCalendar *calender = [NSCalendar currentCalendar];
NSDateComponents *components = [calender components:( NSHourCalendarUnit ) fromDate:dateFromString];
//[components setHour:24*(index-1)];
dateFromString = [calender dateByAddingComponents:components toDate:dateFromString options:0];
NSString *stri = [formatter stringFromDate:dateFromString];
cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
cell.dayDateLabel.text = stri;
//[dateFromString release];
[formatter release];
}
cell.dayTempMaxLabel.text = [[[[#"Max: " stringByAppendingString:[responseTempMaxC objectAtIndex:index-1]] stringByAppendingString:#"°C/"] stringByAppendingString:[responseTempMaxF objectAtIndex:index-1]] stringByAppendingString:#"°F"] ;
cell.dayTempMin_HumidityLabel.text = [[[[#"Min: " stringByAppendingString:[responseTempMinC objectAtIndex:index-1]] stringByAppendingString:#"°C/"] stringByAppendingString:[responseTempMinF objectAtIndex:index-1]] stringByAppendingString:#"°F"] ;
cell.dayConditionLabel.text = [responseWeatherDesc objectAtIndex:index-1];
cell.dayWindLabel.text = [[[[responseWindDirection objectAtIndex:index-1]stringByAppendingString:#" at "]stringByAppendingString:[responseWindspeedMiles objectAtIndex:index-1]] stringByAppendingString:#"mph"];
NSURL *url = [NSURL URLWithString:[responseWeatherIconUrl objectAtIndex:index-1]];
cell.dayImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;}
actually i am parsing weather news from a website
can u tell me how can i solve dis
read following link its may be help u
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
http://blog.webscale.co.in/?p=284
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/