The table view is not working when loading.It is giving an error.Here is the error.
NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1
beyond bounds [0 .. 0]'
Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
uiButtonArray =[[NSMutableArray alloc]init];
[SharedPreferenceUtil saveBoolValueInUserDefaults:YES forKey:#"viewupdate"];
indexval = 0;
if (isNotification)
{
isNotification = NO;
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
//[[UIApplication sharedApplication]cancelAllLocalNotifications];
}
[self getRemarks];
[self updateUI];
// Do any additional setup after loading the view.
}
-(void) viewDidLayoutSubviews
{
[remarksTable setFrame:CGRectMake(5, 190, [[UIScreen mainScreen]bounds].size.width-20, [[UIScreen mainScreen]bounds].size.height-180)];
[Calendarscroll setFrame:CGRectMake(0,90,320, 59)];
[Calendarscroll setBackgroundColor:[UIColor whiteColor]];
Calendarscroll.contentSize =CGSizeMake(1500, 59);
[self.view layoutIfNeeded];
}
-(void)fillcalendar
{
buttonIndex=0;
for (UIButton *btn in uiButtonArray )
{
[btn removeFromSuperview];
}
[uiButtonArray removeAllObjects];
globalDate=currentDate;
// this is the placeholder for the calendar
// setting up the month title
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM yyyy"];
[monthLabel setText:[df stringFromDate:currentDate]];
NSDateComponents *components = [calendar components:NHWunits fromDate:currentDate];
// change for the first day
[components setDay:1];
// update the component
components = [calendar components:NHWunits fromDate:[calendar dateFromComponents:components]];
[components setDay:-[components weekday]+2];
int lessDay = [components day];
int month = [components month];
int year = [components year];
// extracting components from date
NSDateComponents *components3 = [calendar components:NHWunits fromDate:[NSDate date]];
int currentDateInteger=[components3 day];
monthInteger=[components3 month];
yearInteger=[components3 year];
CGFloat orgx=0,orgy=1.0f;
int m=month;
for (int week=0; week<6 && month==m; week++)
{
for (int d=0; d<7; d++)
{
[components setDay:lessDay];
int d =[[calendar components:NHWunits fromDate:[calendar dateFromComponents:components]] day];
m =[[calendar components:NHWunits fromDate:[calendar dateFromComponents:components]] month];
int y =[[calendar components:NHWunits fromDate:[calendar dateFromComponents:components]] year];
NSString *daysdate = [NSString stringWithFormat:#"%d-%d-%d",d,m,y];
NSDateFormatter *nwd =[[NSDateFormatter alloc]init];
[nwd setDateFormat:#"dd-MM-yyyy"];
NSDate *notdays =[nwd dateFromString:daysdate];
[nwd setDateFormat:#"EEE"];
NSString *WDays =[nwd stringFromDate:notdays];
UILabel *Weekdays =[self createLbl:WDays orgx:orgx orgy:orgy];
UIButton *button = [self createBtn:d orgx:orgx orgy:orgy Date:currentDate];
if (d==defaultDateInteger && defaultMonthInteger==m)
{
// globalButton=button;
}
NSString *isSunday =[NSString stringWithFormat:#"%d-%d-%d",d,m,y];
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"d-M-yyyy"];
NSString *cdate = [df stringFromDate:date];
NSLog(#"CURRENT DATE: %# \n %#", isSunday,cdate);
//Circle the Today's Date
if ([isSunday isEqualToString:cdate]) {
[button setBackgroundColor:[UIColor lightGrayColor]];
// [button.titleLabel setTextColor:[UIColor whiteColor]];
}
[Calendarscroll addSubview:button];
[Calendarscroll addSubview:Weekdays];
button.tag=buttonIndex;
[uiButtonArray addObject:button];
// [DayArray addObject:Weekdays];
buttonIndex++;
orgx+=46;
lessDay++;
}
}
}
- (UIButton*) createBtn:(int)day orgx:(CGFloat)orgx orgy:(CGFloat)orgy Date:(NSDate*)caldate
{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
NSDateFormatter *cMonth = [[NSDateFormatter alloc] init];
[cMonth setDateFormat:#"MMMM yyyy"];
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd-MM-yyyy"];
NSDateFormatter *dff = [[NSDateFormatter alloc]init];
[dff setDateFormat:#"d"];
NSDateFormatter *days =[[NSDateFormatter alloc]init];
[days setDateFormat:#"EEE"];
NSString *cdate1 = [dff stringFromDate:date];
NSLog(#"The single date is: %#",cdate1);
NSString *cdate = [df stringFromDate:date];
NSLog(#"df is: %#",cdate);
NSString *cdays =[days stringFromDate:date];
NSLog(#"The days are: %#",cdays);
[button setBackgroundColor:[UIColor colorWithRed:0.52 green:0.5 blue:0.12 alpha:0.8]];
button.titleEdgeInsets = UIEdgeInsetsMake(-14.0, -16.0, 0.0, 0.0);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d", day] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
button.frame=CGRectMake(orgx, 28, 35, 30);
return button;
}
-(UILabel*) createLbl:(NSString*)Days orgx:(CGFloat)orgx orgy:(CGFloat)orgy
{
UILabel *Weekdays =[[UILabel alloc]initWithFrame:CGRectMake(orgx, 5, 35, 20)];
NSDateFormatter *cMonth = [[NSDateFormatter alloc] init];
[cMonth setDateFormat:#"MMMM yyyy"];
NSDate *Ndate = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd-MM-yyyy"];
NSDateFormatter *days =[[NSDateFormatter alloc]init];
[days setDateFormat:#"EEE"];
NSString *cdate = [df stringFromDate:Ndate];
NSLog(#"df is: %#",cdate);
NSString *cdays =[days stringFromDate:Ndate];
NSLog(#"The days are: %#",cdays);
[Weekdays setBackgroundColor:[UIColor colorWithRed:0.52 green:0.5 blue:0.12 alpha:0.8]];
//Weekdays.titleEdgeInsets = UIEdgeInsetsMake(-14.0, -16.0, 0.0, 0.0);
[Weekdays setTextColor:[UIColor blackColor]];
[Weekdays setText:[NSString stringWithFormat:#"%#", Days]];
[Weekdays setFont:[UIFont systemFontOfSize:16.0f]];
return Weekdays;
}
-(void)CreateSection
{
sectionsData =[[NSMutableArray alloc]init];
for (int i=0; i<collectHW.count; i++)
{
/* [sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
// [Sectiondata addObject:sectionarray];
NSLog(#"The description is: %#",sectionsData);*/
//for (int i =0; i<newsArray.count; i++)
if ([[[collectHW objectAtIndex:i] valueForKey:#"path"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
}
else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]], nil]];
}
/* if ([[[collectHW objectAtIndex:i] valueForKey:#"path"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"name"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
}
}else{////deadlinedate........
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
}
}
}else
{//////name........
if ([[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
}
}else{////deadlinedate........
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Tap to download attachment."],#"hasAttachment",nil]];
}
}
}
}else
{/////////path......
if ([[[collectHW objectAtIndex:i] valueForKey:#"name"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],nil]];
// NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],nil]];
}
}else{////deadlinedate........
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],nil]];
// NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"Title: %#",[[collectHW objectAtIndex:i] valueForKey:#"name"]],nil]];
}
}
}else
{//////name........
if ([[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],nil]];
// NSLog(#"The description is: %#",sectionsData);
}else
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DUE DATE: %#",[[collectHW objectAtIndex:i] valueForKey:#"deadlinedate"]],nil]];
}
}else{////deadlinedate........
if ([[[collectHW objectAtIndex:i] valueForKey:#"description"]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:BLANK_STRING]].length!=0)
{
[sectionsData addObject:[[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:#"DESCRIPTION: %#",[[collectHW objectAtIndex:i] valueForKey:#"description"]],nil]];
// NSLog(#"The description is: %#",sectionsData);
}else
{
[self showDropDownViewWithmessage:#"No homework is yet to display." title:#"Message" inView:self.view];
}
}
}
}*/
NSLog(#"The new data is: %#",sectionsData);
}
UIColor *sectionsColor = [UIColor colorWithRed:36.0f/255.0f green:158.0f/255.0f blue:110.0f/255.0f alpha:1.0f];
UIColor *sectionTitleColor = [UIColor whiteColor];
UIFont *sectionTitleFont = [UIFont fontWithName:#"Futura" size:24.0f];
EMAccordionSection *section;
// emTV.closedSectionIcon=[UIImage imageNamed:#"icon_down.png"];
// emTV.openedSectionIcon=[UIImage imageNamed:#"icon_up.png"];
for (int i = 0 ; i<collectHW.count; i++)
{
section = [[EMAccordionSection alloc] init];
[section setBackgroundColor:sectionsColor];
[section setItems:[sectionsData objectAtIndex:i]];
[section setTitle:[NSString stringWithFormat:#"%#(%#)",[[collectHW objectAtIndex:i] valueForKey:#"subjectname"],[[collectHW objectAtIndex:i] valueForKey:#"assignmenttype"]]];
[section setTitleFont:sectionTitleFont];
[section setTitleColor:sectionTitleColor];
[emTV addAccordionSection:section];
}
NSLog(#"The Section is: %#",section.title);
[remarksTable reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"emCell"];
NSMutableArray *items = [sectionsData objectAtIndex:indexPath.section];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(5.0f,0.0f,220.0f, 40.0f)];
[titleLbl setFont:[UIFont boldSystemFontOfSize:14.0f]];
[titleLbl setText:[items objectAtIndex:indexPath.row]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTextColor:[UIColor blackColor]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [[items objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height+10, 40.0f);
[titleLbl setFrame:CGRectMake(30, titleLbl.frame.origin.y, 280.0f, height)];
titleLbl.numberOfLines=30;
[[cell contentView] addSubview:titleLbl];
[cell setBackgroundColor:[UIColor clearColor]];
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//TAHomeworksTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkCell"];
NSMutableArray *items = [sectionsData objectAtIndex:indexPath.section];
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height+15, 40.0f);
return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
NSString *text = [NSString stringWithFormat:#"%#(%#)",[[collectHW objectAtIndex:section] valueForKey:#"subjectname"],[[collectHW objectAtIndex:section] valueForKey:#"assignmenttype"]];
NSLog(#"The text is: %#",text);
CGSize constraint = CGSizeMake(250.0f - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height+35, 40.0f);
return height;
}
- (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
int red = 0;
int green = 0;
int blue = 0;
sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}
-(void)updateUI
{
emTV = [[EMAccordionTableViewController alloc] initWithTable:remarksTable];
[emTV setDelegate:self];
[self addCustomBackButtonWithNaviBarColor:[self colorWithHexString:#"#c3b395" alpha:1.0f]];
//[self addCustomBackButton];
if (IS_IOS7)
{
[remarksTable setSeparatorInset:UIEdgeInsetsZero];
}
[self addCustomBackButtonWithNaviBarColor:[self colorWithHexString:#"#006666" alpha:1.0f]];
NSMutableArray *menuindex = (NSMutableArray *)[SharedPreferenceUtil getNSObject:#"menuindexarray"];
[self addCutomTitleToCustomNavigationBar:[NSString stringWithFormat:#"%#",[(NSMutableArray *)[SharedPreferenceUtil getNSObject:#"appnamearray"]objectAtIndex:[[menuindex objectAtIndex:[(NSString *)[SharedPreferenceUtil getNSObject:#"selmenuitem"]integerValue]]integerValue]]]];
currentDate=[NSDate date];
globalDate=currentDate;
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//today = [[NSDate alloc] initWithTimeIntervalSinceNow:1];
NSDateComponents *components = [calendar components:NHWunits fromDate:currentDate];
defaultDateInteger=components.day;
[self fillcalendar];
// [self getdate];
// [self dateSelectedInPickerView];
[self getRemarks];
}
-(void)getRemarks
{
if ([self connected])
{
[self showLoadingViewWithMessage:#"Loading..."];
[[DPSAppService sharedInstance] getDataFromURL:[self getURL] responseBlock:^(NSDictionary *response)
{
if (response)
{
NSLog(#"%#",response);
homewokArray = [response valueForKey:#"studentassignment"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy"];
NSDate *CurrentDate =[NSDate date];
NSString *cDate =[df stringFromDate:CurrentDate];
NSLog(#" current Month date is: %#",cDate);
collectHW =[[NSMutableArray alloc]init];
for (int i=0; i<homewokArray.count; i++)
{
NSLog(#"New data is: %#",[[homewokArray objectAtIndex:i]valueForKey:#"date"]);
if ([cDate isEqualToString:[[homewokArray objectAtIndex:i]valueForKey:#"date"]])
{
[collectHW addObject:[homewokArray objectAtIndex:i]];
}
else
{
NSLog(#"No Homework today");
}
}
NSLog(#"Current data of homework is: %#",collectHW);
if (homewokArray.count==0)
{
[self showDropDownViewWithmessage:#"No homework is yet to display." title:#"Message" inView:self.view];
}else
{
[self CreateSection];
// [self showHomework];
}
}
else
{
[self showDropDownViewWithmessage:SERVER_ERROR title:ERROR inView:self.view];
}
[self hideLoadingViewWithSuccessMessage:LOGIN_SUCCESS];
[self addBottomView];
}];
}
else
{
}
}
-(NSString *)getURL
{
return [NSString stringWithFormat:#"%#%#%#classid=%#&academciyearid=%#§ionid=%#",[SharedPreferenceUtil getNSObject:#"baseerpurl"],URL,STUDENT_HOMEWORK,[[SharedPreferenceUtil getNSObject:STUDENT_DATA_JSON ]valueForKey:#"classid"],[[SharedPreferenceUtil getNSObject:STUDENT_DATA_JSON ]valueForKey:#"academciyearid"],[[SharedPreferenceUtil getNSObject:STUDENT_DATA_JSON ]valueForKey:#"sectionid"]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Add exceptional breakpoint and see how many objects are there in Array and from which index an object being taken out from that array,by doing this you will be able to find out and sort out the problem :)
You always have to return the correct value in numberOfRowsInSection method. GENERALLY, that value is the count of an array. If this is also your case then please debug your flow and check for inconsistencies. Whenever the count of the array is altered or changed, the tableview should be reloaded.
I am new in IOS development, i want to write down following scenario in best possible way:
- (void)configureDataSource {
NSMutableArray *meterUnitArray = [[NSMutableArray alloc] init];
NSMutableArray *meterSubUnitArray = [[NSMutableArray alloc] init];
NSMutableArray *footUnitArray = [[NSMutableArray alloc] init];
NSMutableArray *footSubUnitArray = [[NSMutableArray alloc] init];
self.footArray = [[NSMutableArray alloc] init];
self.meterArray = [[NSMutableArray alloc] init];
for(int i = 0; i <= 99; i++)
{
NSString *str = [NSString stringWithFormat:#"%d Meter", i];
NSString *str1 = [NSString stringWithFormat:#"%d cm", i];
[meterUnitArray addObject:str];
[meterSubUnitArray addObject:str1];
NSString *str2 = [NSString stringWithFormat:#"%d Foot", i];
NSString *str3 = [NSString stringWithFormat:#"%d Inches", i];
[footUnitArray addObject:str2];
[footSubUnitArray addObject:str3];
}
[self.meterArray addObject:meterUnitArray];
[self.meterArray addObject:meterSubUnitArray];
[self.footArray addObject:footUnitArray];
[self.footArray addObject:footSubUnitArray];
}
How can i refine this code?
#interface YourClass ()
#property (strong, nonatomic) NSArray *footArray;
#property (strong, nonatomic) NSArray *meterArray;
#end
#implementation YourClass
- (void)configureDataSource {
self.meterArray = #[[#[] mutableCopy], [#[] mutableCopy]];
self.footArray = #[[#[] mutableCopy], [#[] mutableCopy]];
for(int i = 0; i <= 99; i++) {
[self.meterArray[0] addObject:[NSString stringWithFormat:#"%d Meter", i]];
[self.meterArray[1] addObject:[NSString stringWithFormat:#"%d cm", i]];
[self.footArray[0] addObject:[NSString stringWithFormat:#"%d Foot", i]];
[self.footArray[1] addObject:[NSString stringWithFormat:#"%d Inches", i]];
}
}
I have one array of array.I want check my inner array is empty or not.and I am checking objects of array are empty string.
Here is my code :
NSMutableArray *newArray = [NSMutableArray array];
NSMutableArray *selectArray = [NSMutableArray array];
for (int i = 0; i < [newArray count]; i++)
{
NSArray *arr = [newArray objectAtIndex:i];
if ([arr count] > 0)
{
NSString *str = [arr objectAtIndex:0];
if (![str isEqualToString:#""])
{
[selectArray addObject:str];
}
}
}
my newArray is array of array.I want to add strings in selectArray which are not empty.but, only 0th index object is added in selectArray multiple times.
Please suggest me, where i am doing wrong.thanks
Thats because you are just getting the 0'th element of your newArray's elements, I mean this line
NSString *str = [arr objectAtIndex:0];
you should put another for in your code. Use this code instead of yours
NSMutableArray *newArray = [NSMutableArray array];
NSMutableArray *selectArray = [NSMutableArray array];
for (int i = 0; i < [newArray count]; i++){
NSArray *arr = [newArray objectAtIndex:i];
for (int j = 0; j < [arr count]; j++){
NSString *str = [arr objectAtIndex:j];
if (![str isEqualToString:#""]){
[selectArray addObject:str];
}
}
}
Use this code You not need to create any extra variable.
for (int i = 0; i < [newArray count]; i++)
{
for (int j = 0; j < [[newArray objectAtIndex:i] count]; j++)
{
if (![[[newArray objectAtIndex:i] objectAtIndex:j] isEqualToString:#""])
{
[selectArray addObject:str];
}
}
}
You can use following generic function to check any of the value or object is empty or null.
-(BOOL)isEmpty:(id)object{
return object == nil
|| [object isKindOfClass:[NSNull class]]
|| ([object respondsToSelector:#selector(length)]
&& [(NSData *)object length] == 0)
|| ([object respondsToSelector:#selector(count)]
&& [(NSArray *)object count] == 0);
}
You can try with this
NSMutableArray *newArray = [NSMutableArray array];
NSMutableArray *selectArray = [NSMutableArray array];
for (NSArray *arrayFromNewArray in newArray) {
if ([arrayFromNewArray count] > 0)
{
NSString *str = [arrayFromNewArray firstObject];
if (![str isEqualToString:#""])
{
[selectArray addObject:str];
}
}
}
You should be
NSMutableArray *newArray = [NSMutableArray array];
NSMutableArray *selectArray = [NSMutableArray array];
for (int i = 0; i < [newArray count]; i++)
{
NSArray *arr = [newArray objectAtIndex:i];
for (int j = 0; j < [arr count]; j++)
{
NSString *str = [arr objectAtIndex:j];
if (![str isEqualToString:#""])
{
[selectArray addObject:str];
}
}
}
Problem is you forgot the second loop :) Let me know if I am correct.
I want to create a table view which is scrollable both horizontally and vertically. I can do this by taking values myself. But I want to take the values from json. I am using model class and my json values are like this:
[
{
"cid": 109,
"iid": 10653,
"yr": 1994,
"val": "15.4311527806175"
},
{
"cid": 109,
"iid": 7872,
"yr": 1999,
"val": "8.84575553637328"
},
{
"cid": 109,
"iid": 7872,
"yr": 1998,
"val": "6.18441582677751"
},
I want my first column to be fixed when scrolling horizontally and first row to be fixed when I scroll it vertically. First column should have iid. first row should have yr and data should have val.
I am using XCMltisortTableView, ASIHttpRequest and SBJson.
I am getting the header value and left column value. How can I get the data corresponding to the yr and iid in proper column?
My code for val in requestFinished method is as follows:
rightTableData = [NSMutableArray arrayWithCapacity:mainTableData.count];
NSMutableArray *right = [NSMutableArray arrayWithCapacity:mainTableData.count];
for (int i = 0; i < mainTableData.count; i++)
{
// Model *model = [mainTableData objectAtIndex:i];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:mainTableData.count];
for (int j = 0; j < headData.count; j++)
{
Model *model = [mainTableData objectAtIndex:j];
if([headData isEqual: #"2000"])
{
[array addObject:[NSString stringWithFormat:#"%#", model.val]];
}
else if([headData isEqual: #"2001"])
{
[array addObject:[NSString stringWithFormat:#"%#", model.val]];
}
else if([headData isEqual: #"2002"])
{
[array addObject:[NSString stringWithFormat:#"%#", model.val]];
}
else if([headData isEqual: #"2003"])
{
[array addObject:[NSString stringWithFormat:#"%#", model.val]];
}
else if([headData isEqual: #"2004"])
{
[array addObject:[NSString stringWithFormat:#"%#", model.val]];
}
else
{
[array addObject:[NSString stringWithFormat:#"%#", model.val]];
}
}
[right addObject:array];
}
[rightTableData addObject:right];
I'm totally new and would a appreciate any answers and any suggestions. Please do reply.
Thanks!
After trying for a long time I got the result using the code below:
-(void) requestFinished: (ASIHTTPRequest *) request
{
NSString *theJSON = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];
headData = [[NSMutableArray alloc] init];
NSMutableArray *head = [[NSMutableArray alloc] init];
leftTableData = [[NSMutableArray alloc] init];
NSMutableArray *left = [[NSMutableArray alloc] init];
rightTableData = [[NSMutableArray alloc]init];
for (NSMutableArray *dictionary in jsonDictionary)
{
Model *model = [[Model alloc]init];
model.cid = [[dictionary valueForKey:#"cid"]intValue];
model.iid = [[dictionary valueForKey:#"iid"]intValue];
model.yr = [[dictionary valueForKey:#"yr"]intValue];
model.val = [dictionary valueForKey:#"val"];
[mainTableData addObject:model];
[head addObject:[NSString stringWithFormat:#"%ld", model.yr]];
[left addObject:[NSString stringWithFormat:#"%ld", model.iid]];
}
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:head];
headData = [[orderedSet array] mutableCopy];
NSOrderedSet *orderedSet1 = [NSOrderedSet orderedSetWithArray:left];
NSMutableArray *arrLeft = [[orderedSet1 array] mutableCopy];
//remove duplicate enteries from header array
[leftTableData addObject:arrLeft];
NSMutableArray *right = [[NSMutableArray alloc]init];
for (int i = 0; i < arrLeft.count; i++)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int j = 0; j < headData.count; j++)
{
/* NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.iid == %ld", [[arrLeft objectAtIndex:i] intValue]];
NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];*/
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.iid == %ld AND SELF.yr == %ld", [[arrLeft objectAtIndex:i] intValue], [[headData objectAtIndex:j] intValue]];
NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];
if([filteredArray count]>0)
{
Model *model = [filteredArray objectAtIndex:0];
[array addObject:model.val];
}
}
[right addObject:array];
}
[rightTableData addObject:right];
}
Hope it helps others.
I am using UISegmentedControl and using UITableView but my query is segment button 1 click and display data for yes and 2 button click and display data from no and adding A to Z section UITableView first button click properly data display and properly tableview section atoz display but 2 button click and app crash `
#import "ZnameViewController.h"
#import "ZpaleoViewController.h"
#import "SWRevealViewController.h"
#interface ZnameViewController ()
#end
#implementation ZnameViewController
#synthesize strname1,sagmentController,objtbl,name;
- (void)viewDidLoad {
[super viewDidLoad];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.slide setTarget: self.revealViewController];
[self.slide setAction: #selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
databse = [FMDatabase databaseWithPath:appdelegate.appDBPath];
[arrCatname removeAllObjects];
[arrPaleo removeAllObjects];
arrCatname = [[NSMutableArray alloc]init];
arrPaleo = [[NSMutableArray alloc]init];
arrStatus = [[NSMutableArray alloc]init];
arrCat2 = [[NSMutableArray alloc]init];
arrSta2 = [[NSMutableArray alloc]init];
[self segmentSwitch:self];
// Do any additional setup after loading the view.
}
- (IBAction)segmentSwitch:(id)sender {
[databse open];
NSString *selectQuery = [NSString stringWithFormat:#"select * from Food ORDER BY name ASC"];
NSLog(#"%#",selectQuery);
FMResultSet *resultQuary = [databse executeQuery:selectQuery];
while ([resultQuary next]) {
// NSString *z_paleo = [resultQuary stringForColumn:#"status"];
z_paleo = [NSString stringWithFormat:#"%d",[resultQuary intForColumn:#"status"]];
if ([z_paleo isEqualToString:#"1"]) {
if(z_name == nil || [z_name isKindOfClass:[NSNull null]])
{
[arrPaleo addObject:#""];
}
else{
[arrPaleo addObject:z_name];
}
[arrSta2 addObject:z_paleo];
}
else{
if(z_name == nil || [z_name isKindOfClass:[NSNull null]])
{
[arrCatname addObject:#""];
}
else
{
[arrCatname addObject:z_name];
}
[arrCat2 addObject:z_paleo];
}
}
[databse close];
if(sagmentController.selectedSegmentIndex == 0)
{
if ([z_paleo isEqualToString:#"1"]) {
[self getName:arrPaleo];
objtbl.hidden = NO;
}
}
else if (sagmentController.selectedSegmentIndex == 1)
{
if ([z_paleo isEqualToString:#"0"]) {
[self getName:arrCatname];
objtbl.hidden = NO;
}
}
[objtbl reloadData];
}
-(void)getName:(NSMutableArray *)arr
{
NSMutableArray *temp = [[NSMutableArray alloc] init];
NSMutableArray *temp2 = [[NSMutableArray alloc] init];
for(int i = 0; i < arr.count; i++)
{
NSString *string = [arr objectAtIndex:i];
dict = [[NSMutableDictionary alloc] init];
[dict setObject:string forKey:#"Name"];
[dict setObject:[NSNumber numberWithInt:i] forKey:#"ID"];
NSString *firstString = [string substringToIndex:1];
if([temp2 containsObject:firstString] == NO || temp2.count == 0)
{
if(temp2.count != 0)
{
[temp addObject:temp2];
temp2 = [[NSMutableArray alloc] init];
}
[temp2 addObject:firstString];
}
[temp2 addObject:dict];
}
[temp addObject:temp2];
sorted = [[NSArray alloc] initWithArray:temp];
}
-(void)getname2:(NSMutableArray *)array{
NSMutableArray *temp = [[NSMutableArray alloc] init];
NSMutableArray *temp2 = [[NSMutableArray alloc] init];
for(int i = 0; i < array.count; i++)
{
NSString *string = [array objectAtIndex:i];
dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:string forKey:#"Name"];
[dict1 setObject:[NSNumber numberWithInt:i] forKey:#"ID"];
NSString *firstString = [string substringToIndex:1];
if([temp2 containsObject:firstString] == NO || temp2.count == 0)
{
if(temp2.count != 0)
{
[temp addObject:temp2];
temp2 = [[NSMutableArray alloc] init];
}
[temp2 addObject:firstString];
}
[temp2 addObject:dict1];
}
[temp addObject:temp2];
sorted1 = [[NSArray alloc] initWithArray:temp];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
int i = 0;
for(NSArray *array in sorted)
{
NSString *string = [array objectAtIndex:0];
if([string compare:title] == NSOrderedSame)
break;
i++;
}
return i;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (sagmentController.selectedSegmentIndex == 0) {
return [sorted count];
}else {
return [sorted count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *array = [sorted objectAtIndex:section];
return [array objectAtIndex:0];
//return [foodIndexTitles objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *titleArray = [NSMutableArray array];
[titleArray addObject:#"A"];
[titleArray addObject:#"B"];
[titleArray addObject:#"C"];
[titleArray addObject:#"D"];
[titleArray addObject:#"E"];
[titleArray addObject:#"F"];
[titleArray addObject:#"G"];
[titleArray addObject:#"H"];
[titleArray addObject:#"I"];
[titleArray addObject:#"J"];
[titleArray addObject:#"K"];
[titleArray addObject:#"L"];
[titleArray addObject:#"M"];
[titleArray addObject:#"N"];
[titleArray addObject:#"O"];
[titleArray addObject:#"P"];
[titleArray addObject:#"Q"];
[titleArray addObject:#"R"];
[titleArray addObject:#"S"];
[titleArray addObject:#"T"];
[titleArray addObject:#"U"];
[titleArray addObject:#"V"];
[titleArray addObject:#"W"];
[titleArray addObject:#"X"];
[titleArray addObject:#"Y"];
[titleArray addObject:#"Z"];
return titleArray;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (sagmentController.selectedSegmentIndex == 1) {
NSArray *array = [sorted objectAtIndex:section];
return (array.count - 1);
}else{
NSArray *array = [sorted objectAtIndex:section];
return (array.count - 1);
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifire = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifire];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
}
if (sagmentController.selectedSegmentIndex == 0) {
NSArray *array = [sorted objectAtIndex:indexPath.section];
dict = [array objectAtIndex:indexPath.row + 1];
cell.textLabel.text = [dict objectForKey:#"Name"];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Yes_check.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 15, 15)];
}
else
{
NSArray *array = [sorted objectAtIndex:indexPath.section];
dict = [array objectAtIndex:indexPath.row + 1];
cell.textLabel.text = [dict objectForKey:#"Name"];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"No.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 15, 15)];
return cell;
}
return cell;
}