hi, i am making an application in iphone, in which i am creating tableview with 2 sections. i have created sections parts of tableview, but the problem is that i need different images in every cell of each section. anyone has any solution for this
+1
A:
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.
Tim van Elsloo
2009-08-19 10:19:30
hi timi have tried ur code but it's giving me the 4 error in every line of [cell.imageView setimage:.....] "imageView is not a structure or union"
2009-08-19 10:33:51
Well, in that case, I guess you have an older version of the iPhone SDK. Do you have 3.0 or higher? Else you have to do it as following: [cell setImage:... and change cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; in cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
Tim van Elsloo
2009-08-19 15:42:59
thanx i am earlier using 2.2.1. dts why its giving me error. it works perfectly in 3.0
2009-08-20 09:13:23
nw, i want to show selected images to next view. how'll i do it
2009-08-20 09:24:09
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {UIViewController *vc = [[UIViewController alloc] init];UIImageView *iv = [[UIImageView alloc] initWithImage:[[tableView cellForRowAtIndexPath:indexPath].imageView image]];[vc setView:iv];[yourNavController pushViewController:vc animated:TRUE];}
Tim van Elsloo
2009-08-20 10:32:53
hi time it's working, but image is taking full view space i need it take only space which i hav take uiimageview in next view nib file i hav txt 2 which i hav already showed it in next view. but your code hiding the txt value. so hw'll get rid of ds. thanx 4 d reply
2009-08-20 10:51:51