Add images to table cells in sections

3k Views Asked by At

i want to display an image besides the text in table cell. i have 2 sections, General and Assignments. Now i only want to display image beside Assignments and no image besides General section. the below code displays images for Assignments section and then again repeats the images for General section. Can anybody help me how to accompalish this.

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad {

    staffImages = [[NSMutableArray alloc] init];

    NSArray *arrTemp1 = [[NSArray alloc] initWithObjects:@"TRANSPORTATION",@"ROOMS",@"ACTIVITIES",nil];
    NSArray *arrTemp2 = [[NSArray alloc] initWithObjects:@"CHAT",@"SEARCH",@"CALL",@"MORE",nil];

    NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:arrTemp1,@"Assignments",arrTemp2,
                                                                                   @"General",nil];

    //Add item images
    [staffImages addObject:@"transportation.png"];
    [staffImages addObject:@"hotel.png"];
    [staffImages addObject:@"activities.png"];


    //Set the title
    self.navigationItem.title = @"STAFF ASSIGNMENTS";
    self.tableContents=temp;
    [temp release];

    self.sortedKeys =[self.tableContents allKeys];
    [arrTemp1 release];
    [arrTemp2 release];

    [super viewDidLoad];
}


- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                         reuseIdentifier:SimpleTableIdentifier] autorelease];

        }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];

    cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];

    return cell;
    }
3

There are 3 best solutions below

1
On BEST ANSWER

Assuming the general section is section 0 (i.e. first section)

   if (indexPath.section == 1) {
    cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];

   }
0
On
NSMutableData *data=[NSMutableData dataWithContentsOfURL:[NSURL URLWithString:[NSMutableString stringWithFormat:@"%@",[[dataArray objectAtIndex:indexPath.row]objectForKey:@"LINK"]]]];

//NSLog(@"%@",data);

UIImage *img=[UIImage imageWithData:data];

cell.imageView.image=img;

this is best way

0
On

You need to check the section of the indexPath. You probably will also need to use the section check to make sure that you set the text of the cell to the right value.

//You do not need to actually define this value
//this is just for naming purposes
#define SECTION_ASSIGNMENTS 0

...

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                         reuseIdentifier:SimpleTableIdentifier] autorelease];

        }
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    if(indexPath.section == SECTION_ASSIGNMENTS)
    {
        cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
    }
    return cell;
}