Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2010, 09:57 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 14
patrik1 is on a distinguished road
Default how to make row selected on different image click

iam building an application in which iam showing the 5 small images in a single row of the UITable view cell. i want to make selection of row on each image clicked not on the selection of row .

(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath { return 160; }
(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpsons"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"simpsons"] autorelease]; } //cell.textLabel.text = [theSimpsons objectAtIndex:indexPath.row]; //NSString *imageName = [NSString stringWithFormat:@"%d.png", indexPath.row]; //cell.imageView.image = [UIImage imageNamed:imageName];

image1= [[UIImageView alloc] initWithFrame:CGRectMake(5.0,10.0,78, 74)];
[cell.contentView addSubview:image1]; image1.image = [UIImage imageNamed:@"0.png"];

image2= [[UIImageView alloc] initWithFrame:CGRectMake(80.0,10.0,78, 74)];
[cell.contentView addSubview:image2]; image2.image = [UIImage imageNamed:@"1.png"];

image3= [[UIImageView alloc] initWithFrame:CGRectMake(170.0,10.0,78, 74)];
[cell.contentView addSubview:image3]; image3.image = [UIImage imageNamed:@"2.png"];

image4= [[UIImageView alloc] initWithFrame:CGRectMake(240.0,10.0,78, 74)];
[cell.contentView addSubview:image4]; image4.image = [UIImage imageNamed:@"3.png"]; return cell;

}

i want on each image selection only a new row will we opened not on row click ?????
patrik1 is offline   Reply With Quote
Old 06-30-2010, 10:57 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by patrik1 View Post
iam building an application in which iam showing the 5 small images in a single row of the UITable view cell. i want to make selection of row on each image clicked not on the selection of row .

(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath { return 160; }
(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpsons"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"simpsons"] autorelease]; } //cell.textLabel.text = [theSimpsons objectAtIndex:indexPath.row]; //NSString *imageName = [NSString stringWithFormat:@"%d.png", indexPath.row]; //cell.imageView.image = [UIImage imageNamed:imageName];

image1= [[UIImageView alloc] initWithFrame:CGRectMake(5.0,10.0,78, 74)];
[cell.contentView addSubview:image1]; image1.image = [UIImage imageNamed:@"0.png"];

image2= [[UIImageView alloc] initWithFrame:CGRectMake(80.0,10.0,78, 74)];
[cell.contentView addSubview:image2]; image2.image = [UIImage imageNamed:@"1.png"];

image3= [[UIImageView alloc] initWithFrame:CGRectMake(170.0,10.0,78, 74)];
[cell.contentView addSubview:image3]; image3.image = [UIImage imageNamed:@"2.png"];

image4= [[UIImageView alloc] initWithFrame:CGRectMake(240.0,10.0,78, 74)];
[cell.contentView addSubview:image4]; image4.image = [UIImage imageNamed:@"3.png"]; return cell;

}

i want on each image selection only a new row will we opened not on row click ?????
You're sentences are hard to follow. I gather you are not a native speaker of English?

I gather that you want to have table view cells with 5 small images in each cell, and you want the user to be able to tap on the images and have each image do something special, rather than just selecting the whole cell.

Is that right?

In that case, create custom UIButton objects that include a UIImage, and add actions to those buttons. Each button will trigger a custom action. Even better, the system will highlight the buttons when the user clicks on them, which gives the user good visual feedback.

You can make the cells not selectable if you want, so the only thing they can tap on is the image buttons, not the rest of the cell.


Duncan

Your cellForRowAtIndexPath method above has problems by the way. You check to see if there is an old cell you can reuse using the dequeueReusableCellWithIdentifier method, which is good.

However, you should be aware that the cell you get back will be in the state it was in when it scrolled off the screen. If it already had 5 image views or buttons added to it, those image views/buttons will still be there. You should change the images in them rather than adding 5 more. Change your code so that creating a new cell adds 5 buttons/UIImageViews to the cell, but if you get a recycled cell, you assume the images are already there, and just switch their images.

You can find the image views/buttons in the cell by giving them tags, and using the method viewWithTag. Something like this:


Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
	UIImageView* image1;
	UIImageView* image2;
	UIImageView* image3;
	UIImageView* image4;
	
	UIImageView* anImageView;
	int index;
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpsons"]; 
	
	if (cell == nil) 
	{ 
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
									   reuseIdentifier:@"simpsons"] autorelease]; 
		
		//Only add the imageViews (or buttons) if we are creating a cell.
		image1= [[UIImageView alloc] initWithFrame:CGRectMake(5.0,10.0,78, 74)]; 
		image1.tag = 1;
		[cell.contentView addSubview:image1];
		
		image2= [[UIImageView alloc] initWithFrame:CGRectMake(80.0,10.0,78, 74)]; 
		image2.tag = 2;
		[cell.contentView addSubview:image2];
		
		image3= [[UIImageView alloc] initWithFrame:CGRectMake(170.0,10.0,78, 74)]; 
		image3.tag = 3;
		[cell.contentView addSubview:image3];
		
		image4= [[UIImageView alloc] initWithFrame:CGRectMake(240.0,10.0,78, 74)]; 
		image4.tag = 4;
		[cell.contentView addSubview:image4];
	}
	else 
	{
		//This cell is recycled, so it will already have the image views/buttons we added when we created it.
	}

	//The code below will be run whether we create a new cell or recycle an old cell.
	//Change the images in the image views to the images for this cell.
	for (index = 1;index <=4;index++)
	{
		anImageView = [cell.contentView viewWithTag: index - 1];
		imageName = [NSString stringWithFormat: @"%d.png", index];
		anImageView.image = [UIImage imageNamed: imageName];
	}
 }
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-01-2010, 12:33 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 14
patrik1 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You're sentences are hard to follow. I gather you are not a native speaker of English?

I gather that you want to have table view cells with 5 small images in each cell, and you want the user to be able to tap on the images and have each image do something special, rather than just selecting the whole cell.

Is that right?

In that case, create custom UIButton objects that include a UIImage, and add actions to those buttons. Each button will trigger a custom action. Even better, the system will highlight the buttons when the user clicks on them, which gives the user good visual feedback.

You can make the cells not selectable if you want, so the only thing they can tap on is the image buttons, not the rest of the cell.


Duncan

Your cellForRowAtIndexPath method above has problems by the way. You check to see if there is an old cell you can reuse using the dequeueReusableCellWithIdentifier method, which is good.

However, you should be aware that the cell you get back will be in the state it was in when it scrolled off the screen. If it already had 5 image views or buttons added to it, those image views/buttons will still be there. You should change the images in them rather than adding 5 more. Change your code so that creating a new cell adds 5 buttons/UIImageViews to the cell, but if you get a recycled cell, you assume the images are already there, and just switch their images.

You can find the image views/buttons in the cell by giving them tags, and using the method viewWithTag. Something like this:


Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
	UIImageView* image1;
	UIImageView* image2;
	UIImageView* image3;
	UIImageView* image4;
	
	UIImageView* anImageView;
	int index;
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpsons"]; 
	
	if (cell == nil) 
	{ 
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
									   reuseIdentifier:@"simpsons"] autorelease]; 
		
		//Only add the imageViews (or buttons) if we are creating a cell.
		image1= [[UIImageView alloc] initWithFrame:CGRectMake(5.0,10.0,78, 74)]; 
		image1.tag = 1;
		[cell.contentView addSubview:image1];
		
		image2= [[UIImageView alloc] initWithFrame:CGRectMake(80.0,10.0,78, 74)]; 
		image2.tag = 2;
		[cell.contentView addSubview:image2];
		
		image3= [[UIImageView alloc] initWithFrame:CGRectMake(170.0,10.0,78, 74)]; 
		image3.tag = 3;
		[cell.contentView addSubview:image3];
		
		image4= [[UIImageView alloc] initWithFrame:CGRectMake(240.0,10.0,78, 74)]; 
		image4.tag = 4;
		[cell.contentView addSubview:image4];
	}
	else 
	{
		//This cell is recycled, so it will already have the image views/buttons we added when we created it.
	}

	//The code below will be run whether we create a new cell or recycle an old cell.
	//Change the images in the image views to the images for this cell.
	for (index = 1;index <=4;index++)
	{
		anImageView = [cell.contentView viewWithTag: index - 1];
		imageName = [NSString stringWithFormat: @"%d.png", index];
		anImageView.image = [UIImage imageNamed: imageName];
	}
 }
ya .. absolutely right sir , first of all I am sorry my words are not clear to you . ya i want each image must be a button and on clicking the image it will do some action or open another view controller class.

ok i clear it to u fully .

actually i cannot create UIButton , because data comes from the JSON and it will come as a UIImage. iam attaching a screenshot also . in this tableview u can see there are 5 images in each cell which gets updated from the API of some website . i want a UIImage inside the cell and when we click on an image a new view controller class will be opened with a full view of the image . i think now you got it what i want to say and trying to do ?

Last edited by patrik1; 07-01-2010 at 12:44 AM.
patrik1 is offline   Reply With Quote
Old 07-01-2010, 12:47 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 14
patrik1 is on a distinguished road
Default

ya the way you told me that worked ...


i did this way ....

**********************

btn1=[[UIButton alloc]initWithFrame:CGRectMake(5.0,10.0,78, 74)];
[btn1 setImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(UIButton forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview: btn1];
[btn1 release];


- (IBAction)UIButtonid)sender
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Instructions" message:@"..." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}


on clicking the button this alert view is occurring .

thanx duncan
Attached Images
File Type: jpg Screen shot 2010-07-01 at 11.12.07 AM.jpg (8.6 KB, 8 views)

Last edited by patrik1; 07-01-2010 at 05:08 AM.
patrik1 is offline   Reply With Quote
Old 04-13-2011, 05:14 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 3
mushtaque87 is on a distinguished road
Default hey duncan you code is fine but its not executing , there is an error

Quote:
Originally Posted by Duncan C View Post
You're sentences are hard to follow. I gather you are not a native speaker of English?

I gather that you want to have table view cells with 5 small images in each cell, and you want the user to be able to tap on the images and have each image do something special, rather than just selecting the whole cell.

Is that right?

In that case, create custom UIButton objects that include a UIImage, and add actions to those buttons. Each button will trigger a custom action. Even better, the system will highlight the buttons when the user clicks on them, which gives the user good visual feedback.

You can make the cells not selectable if you want, so the only thing they can tap on is the image buttons, not the rest of the cell.


Duncan

Your cellForRowAtIndexPath method above has problems by the way. You check to see if there is an old cell you can reuse using the dequeueReusableCellWithIdentifier method, which is good.

However, you should be aware that the cell you get back will be in the state it was in when it scrolled off the screen. If it already had 5 image views or buttons added to it, those image views/buttons will still be there. You should change the images in them rather than adding 5 more. Change your code so that creating a new cell adds 5 buttons/UIImageViews to the cell, but if you get a recycled cell, you assume the images are already there, and just switch their images.

You can find the image views/buttons in the cell by giving them tags, and using the method viewWithTag. Something like this:


Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
	UIImageView* image1;
	UIImageView* image2;
	UIImageView* image3;
	UIImageView* image4;
	
	UIImageView* anImageView;
	int index;
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"simpsons"]; 
	
	if (cell == nil) 
	{ 
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
									   reuseIdentifier:@"simpsons"] autorelease]; 
		
		//Only add the imageViews (or buttons) if we are creating a cell.
		image1= [[UIImageView alloc] initWithFrame:CGRectMake(5.0,10.0,78, 74)]; 
		image1.tag = 1;
		[cell.contentView addSubview:image1];
		
		image2= [[UIImageView alloc] initWithFrame:CGRectMake(80.0,10.0,78, 74)]; 
		image2.tag = 2;
		[cell.contentView addSubview:image2];
		
		image3= [[UIImageView alloc] initWithFrame:CGRectMake(170.0,10.0,78, 74)]; 
		image3.tag = 3;
		[cell.contentView addSubview:image3];
		
		image4= [[UIImageView alloc] initWithFrame:CGRectMake(240.0,10.0,78, 74)]; 
		image4.tag = 4;
		[cell.contentView addSubview:image4];
	}
	else 
	{
		//This cell is recycled, so it will already have the image views/buttons we added when we created it.
	}

	//The code below will be run whether we create a new cell or recycle an old cell.
	//Change the images in the image views to the images for this cell.
	for (index = 1;index <=4;index++)
	{
		anImageView = [cell.contentView viewWithTag: index - 1];
		imageName = [NSString stringWithFormat: @"%d.png", index];
		anImageView.image = [UIImage imageNamed: imageName];
	}
 }
anImageView = [cell.contentView viewWithTag: index - 1]; there is an error in this lone can you plz correct it... its not executing i believe.
mushtaque87 is offline   Reply With Quote
Old 04-13-2011, 07:49 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by mushtaque87 View Post
anImageView = [cell.contentView viewWithTag: index - 1]; there is an error in this lone can you plz correct it... its not executing i believe.

What do you mean "there is an error"... "its not executing..." Do you get a compile error, or does it fail to do what you expect at runtime?

Does it crash at runtime, or just not do what you want? Does it return nil?

The most likely cause of the line returning nil is that there are no views in the content view with the specified tag, but you haven't given enough information.

This sort of thing is really hard to debug remotely. You need to learn to troubleshoot code yourself.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, objective c, uiimage, uitableview cell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 328
7 members and 321 guests
anothermine, Chickenrig, Domele, givensur, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0