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 .
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 .
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];
}
}
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.
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 ?
hey duncan you code is fine but its not executing , there is an error
Quote:
Originally Posted by Duncan C
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.
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.
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.