Harita
Harita

Reputation:

sectioned UITableView

I am making an application for the iPhone, in which I am creating UITableView with 2 sections. I have created sections parts of UITableView, but the problem is that I need different images in every cell of each section. Does anyone has any solution for this

Upvotes: 0

Views: 1166

Answers (1)

cutsoy
cutsoy

Reputation: 10261

Yes, that's very easy. Have you this in your datasource yet:?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Add the following code to it:

NSString *CellIdentifier = [NSString stringWithFormat:@"cell_%i",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    if (indexPath.section == 0 && indexPath.row == 0){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage.png"]];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage1.png"]];
    }else if (indexPath.section == 1 && indexPath.row == 0){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage2.png"]];
    }else if (indexPath.section == 1 && indexPath.row == 1){
        [cell.imageView setImage:[UIImage imageNamed:@"justanimage3.png"]];
    }
}

You can also save your data in a NSMutableArray, but that's a little bit more complicated.

Upvotes: 1

Related Questions