0

This here is the last update. It is crashing when setting texts in indexpath.section ==0 and ==1 and ==2 with the error

[Home objectAtIndex:]: unrecognized selector sent to instance 0x109624f20 2014-07-30 12:03:24.732 TYM-APP[1704:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Home objectAtIndex:]: unrecognized selector sent to instance 0x109624f20'

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
NSLog(@"wsolna hon");
static NSString *cellIdentifier = @"HomeTableViewCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

// Configure the cell...
int row= [indexPath row];


                if (indexPath.section == 0){
                     homeObject = [homeArray[0] objectAtIndex:indexPath.row];

                         NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[homeObject.News dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
                    cell.NewsDateLabel.text= homeObject.News_Date;
                    cell.NewsLabel.attributedText= attrStr;
                    NSLog(@"news: %@", attrStr);

                }


                if (indexPath.section == 1){
                    homeObject = [homeArray[1] objectAtIndex:indexPath.row];

                  NSLog(@"value of indexpath for library: %d",row);
                         NSLog(@"library: %@",homeObject.News);
                    cell.NewsLabel.text= homeObject.Library_text;
                    cell.TestimonialNameLabel.text= @"";

            }

                if (indexPath.section == 2){
                    homeObject = [homeArray[2] objectAtIndex:indexPath.row];

                         NSLog(@"news: %@",homeObject.Testimonial_Description);
                    cell.NewsLabel.text= homeObject.Library_text;
                    cell.TestimonialNameLabel.text = homeObject.Testimonial_Name;

            }


return cell;
}
2
  • Don't query the database every time you are asked for a cell. Query once and cache the results. Log the results array. How many duplicate objects and modules are there? Commented Jul 30, 2014 at 7:10
  • the first section is being diplayed properly but is being duplicated in every other section Commented Jul 30, 2014 at 7:14

2 Answers 2

1

If the quantity of sections is fixed, I would recommend you to store your data in multidimensional array. Thus it will protect you from the most of logical mistakes later.

// Declaring array for 3 sections
homeArray = [[NSMutableArray alloc] initWithObjects:[[NSMutableArray alloc] init], [[NSMutableArray alloc] init], [[NSMutableArray alloc] init], nil];

Then perform necessary logical separation during retrieving data and placing it into array

UPDATE: As @Wain said, it's not recommended to do any requests inside cellForRowAtIndexPath:, so it will be better to place next snippet in that place where you are caching your information

if ([moduleID isEqualToString:@"77"]){
    [homeArray[0] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
} else if ([moduleID isEqualToString:@"81"]){
    [homeArray[1] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
} else if ([moduleID isEqualToString:@"78"]){
    [homeArray[2] addObject:[[Home alloc]initWithItemID: modID andNewsName:nText andNewsDate: (NSString *) nDate andLibraryText: dText andTestDescription: tText andTestimonialName: (NSString *) tName]];
}

And, finally, get necessary homeObject for your table this way

if (indexPath.section == 0) {
    homeObject = [homeArray[0] objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
    homeObject = [homeArray[1] objectAtIndex:indexPath.row];
} else if (indexPath.section == 2) {
    homeObject = [homeArray[2] objectAtIndex:indexPath.row];
}
Sign up to request clarification or add additional context in comments.

11 Comments

i am getting this error : index 0 beyond bounds for empty array' can i edit my question to the new code to show you the code if you don't mind?
@ralph sure, no problems. Where exactly did you get this error?
@ralph You forgot to update array initialization, it's not 2-dimensional now. Change homeArray = [[NSMutableArray alloc]init]; to homeArray = [[NSMutableArray alloc] initWithObjects:[[NSMutableArray alloc] init], [[NSMutableArray alloc] init], [[NSMutableArray alloc] init], nil];
ok now i have this problem: Home objectAtIndex:]: unrecognized selector sent to instance 0x10926eca0 2014-07-30 11:01:51.337 TYM-APP[1419:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Home objectAtIndex:]: unrecognized selector sent to instance 0x10926eca0'
i think it is at this line: homeObject = [homeArray[0] objectAtIndex:indexPath.row];
|
0

This is a cell reuse problem.

To fix, ensure that each time you return a cell from cellForRowAtIndexPath: you always set the text label contents for all labels. That means explicitly setting some with text and setting the others to have no text.

The alternative (and cleaner) solution is to use different custom cells for each section.

Also, don't query the database every time you are asked for a cell. Query once and cache the results. It's very wasteful to query each time and will make your app run slower.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.