Question

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;
}
Was it helpful?

Solution

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];
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top