can not update an image of a cell in table view - iphone

I am doing customized cell for uitable view like below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"overViewCell";
tableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ( tableCell == nil ) {
[[NSBundle mainBundle] loadNibNamed:#"OverViewCell" owner:self options:nil];
tableCell = overViewCell;
self.overViewCell = nil;
}
..............................................................
}
and OverViewCell.xib looks like ( the right image is tagged as 3 )
And later, I would like to change the image of particular cell from the table view by doing this
- (void) updateImageView {
// currentRow is getting value when cellForRowAtIndex is clicked
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentRow inSection:0];
UITableViewCell *cell = [self.tableContent cellForRowAtIndexPath:indexPath];
imageView = (UIImageView*)[cell viewWithTag:3];
imageView.image = [UIImage imageNamed:#"checked.png"];
[self dismissViewControllerAnimated:YES completion:nil];
}
However, the image is not changed at all
What I am missing in the middle... Please help if you have any clues about this issue.

I figured what the problem is. it is because I was assigning the wrong tag to that image

Related

How to change image of cell?

I have custom cell in iPhone which have which have a button and image view i want to change the image of image view when i clicked on the button here is my button code
-(void)btnLikePressed:(UIButton *)sender{
imageDC *objMenu = [dataArray objectAtIndex:sender.tag];
GET_DBHANDLER
[dbHandler performSelector:#selector(like_image:) withObject: objMenu.image_id];
}
To your method to find out the exact index of your UITableViewCell please use the following :-
CGPoint point = [sender convertPoint:CGPointZero toView:_iTableView];
NSIndexPath *indexPath = [_iTableView indexPathForRowAtPoint:point];
You will get your indexpath. From here you can identify your UIImageView, hope you have given tags to them and then change the image.
try this bellow code
-(void)btnLikePressed:(UIButton *)sender{
static NSString *CellIdentifier = #"yourCellName";
UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
UITableView *tableView=(UITableView*)[cell superview];
NSIndexPath *path=[tableView indexPathForCell:cell];
yourCellName *selectedCell = (yourCellName*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[selectedCell.imageView.image setImage:[UIImage imageNamed:#"yourImageName.png"]];
}
First give tagValue to your imageView , like you gave 5 to your imageView.
Then access this on buttonClick.
-(void)btnLikePressed:(UIButton *)sender
{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
UIImageView*imageView=(UIImageView*)[cell viewWithTag:5];
//Now do whatEver you want to do with your imageview
}
EDIT
If you added your imageView as
[cell.contentView addSubview:imageView];
Then get your tableView cell like this
UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
Following steps will work:-
tag your imageview.
use this code in cellForRowatIndexpath
NSArray *nib;
static NSString *cellIdentifier= #"cell";
UITableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(theCell== nil)
{
{
nib = [[NSBundle mainBundle] loadNibNamed:<Your cellNibName> owner:self options:nil];
}
theCell = [nib objectAtIndex:0];
theCell.selectionStyle = UITableViewCellSelectionStyleNone;
// theCell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
UIImageView *imageView=(UIImageView*)[[theCell contentView] viewWithTag:<Your tag>];
//do what ever you want with imageView
You got image view ref do what you want.
In UITableViewDataSource function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
while setting cell just add tag to your button and imageView as well.
cell.yourBtn.tag = indexPath.row;
cell.imageView.tag = indexPath.row + 1000(any number);
now in your UITableViewController class
now when user click on the button you get the button tag that is actually your row number and imageView(btn tag + 1000) tag as well that you can get imageView by using same info.
-(void)btnLikePressed:(UIButton *)sender{
NSInteger btnTag = [sender tag];
UIImageView *imageView = [yourTableView viewWithTag:btnTag + 1000];
// here your code by using imageView
}

Custom Cell UITableview

I want to have a custom cell in my UITableview. I have tried it with the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell*)view;
}
}
}
[cell setLabelText:[daten objectAtIndex:indexPath.row]];
return cell;
}
But I am getting an exception on that line:
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I'm almost sure that you have lost connection in your XIB file. If commentLabel is still exists you should either delete it and create a new outlet or check the outlets section and delete extra connection and all will be alright!!
EDIT:
This error occurs when you have two outlets connected to the same label in IB.
This is how I made my custom cell and called it from the class :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = customCell;
customCell = nil;
}
// Configure the cell.
cell.serialLabel.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
if u are not using the latest ios SDK, make sure that u have written
#synthesize commentLabel;
in CustomCell
Try cleaning the build folder, or try Simulator -> Reset Content and Settings.

change cell content view when custome view added to cell

I have created one uiviewcontroller with xib to adding in the uitableview cell. here is the code for that.
#interface CustomeCellHome : UIViewController{
IBOutlet UIImageView *imgViewBack;
// will have number of labels and imageviews.
}
#property (nonatomic, retain) UIImageView *imgViewBack;
#implementation CustomeCellHome
#synthesize imgViewBack;
all this IBOutlet connected to xib.
now i am adding this to my uitableview cell. here is code for that.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// }
CustomeCellHome *objCell = [[CustomeCellHome alloc] init];
[objCell.view setTag:indexPath.row];
[cell.contentView addSubview:objCell.view];
objCell.imgViewBack.image = [UIImage imageNamed:#"unsel.png"];
[objCell release];
return cell;
}
now i want to change this imgViewBack image on selection of row. here is code for that.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CustomeCellHome *objCCell = [[CustomeCellHome alloc] init];
objCCell.view = [cell viewWithTag:indexPath.row];
objCCell.imgViewBack.image = [UIImage imageNamed:#"sel.png"];
}
but my imgViewBack is not changing its image. don't getting what is wrong with this. any suggestion for that ? any suggestion will be appreciated .
If you must use a viewController like u do (again probably this is not what u need), I can see few problems with your code.
1) Instead of [objCell.view setTag:indexPath.row]; use [objCell.view setTag:indexPath.row+1]; otherwise u will have a problem when indexPath.row=0 (I think 0 is also the tag of the superview).
2) Don't release the viewController, if u need one keep it around.
(but u will need to save it in some data structure so later u can release it...)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = nil;
// u will have problems reusing those cells...
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if(cell == nil)
// {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CustomeCellHome *objCell = [[CustomeCellHome alloc] init];
[objCell.view setTag:indexPath.row+1];
[cell.contentView addSubview:objCell.view];
objCell.imgViewBack.image = [UIImage imageNamed:#"image1.png"];
//[objCell release]; // don't release now, add it to some data structure so u can release later
// }
return cell;
}
2) Then in didSelectRowAtIndexPath retrieve the cell's viewController and change his imgViewBack property.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// get the cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// get the cell's viewController
UIView *aView = [cell.contentView viewWithTag:indexPath.row+1];
CustomeCellHome *objCCell = (CustomeCellHome *)[aView nextResponder];
// update the image
objCCell.imgViewBack.image = [UIImage imageNamed:#"image2.png"];
}
This will change the image, but this is not a perfect solution, u will have problems if u r reusing cells... Try rethink if u really need to use the viewController.

UIStepper in uitableviewcell

In my app I have custom UITableViewCell, and I have UIStepper and UILabel in the custom cell. I don't know how to check which stepper was clicked. So is it a way to know from which cell stepper was clicked?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
if ([nib count] > 0) {
cell = self.tbcell;
}
}
return cell;
}
An alternative is:
In the method that gets fired when the UIStepper value changes (e.g. #max_, above), you can do the following:
- (IBAction)stepperValueDidChanged:(UIStepper *)sender {
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
// assuming your view controller is a subclass of UITableViewController, for example.
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
[step addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
- (void) someAction:(UIStepper *) stepper {
NSLog(#"stepper clicked");
}

Custom UITableViewCell with button

I subclassed UITableViewCell and added a button to it. How do I response to the event when the button is touched? I also need to know which index path was the button pressed on.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:#"CustomTableCell"
owner:nil options:nil];
for (id currentObjects in topLevelObjects){
if ([currentObjects isKindOfClass:[StatusTableCell class]]){
cell = (StatusTableCell *) currentObjects;
break;
}
}
}
cell.customButton.tag = indexPath.row;
return cell;
}
If you have added the UIButton by code in your UITableViewCell Subclass you can use the following code to add an action for it.
//In UITableViewCell subclass
[customButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
And then you have already written the code for accessing the row number by setting,
cell.customButton.tag = indexPath.row;
You can get the tag value in the aMethod and access the indexPath.row value like below,
//In UITableViewCell subclass
-(IBAction)aMethod:(UIButton*)button
{
NSLog(#"indexPath.row value : %d", button.tag);
}
You can assign the IBAction in the CustomTableCell Nib and in the function do something like this:
- (IBAction) btnDoSomethingPressed:(id)sender {
NSIndexPath *indexPath = [yourTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
//Use indexPath (or indexPath.row) like you would in a UITableViewDelegate method
}
This assumes that yourTableView is an instance variable assigned to your table.