The question sums it up-- I'm trying to have a box get checked when someone clicks on the UITableViewCell, which involves changing the image in the cell. The table is set up well and works just fine-- it's displaying the information that I want it to, and the cells select like they should. However I can't get the image to change when the method didSelectRowAtIndexPath is called.
Here's what I have so far. I've removed extraneous code (from other tables, etc), but this should be everything that's relevant.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == diabetesVisitTable) {
if (indexPath.section==0) {
cell.textLabel.text = [_microvascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==1) {
cell.textLabel.text = [_macrovascularValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
else if (indexPath.section==2) {
cell.textLabel.text = [_bloodSugarValues objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Family Med Unchecked Box.jpg"];
}
cell.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
self.diabetesVisitTable.backgroundColor = [UIColor colorWithRed:0.75 green:0.52 blue:0.53 alpha:1.0];
return cell;
}
else return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.imageView.image = [UIImage imageNamed:#"Family Med Checked Box.jpg"];
}
In your tableView:didSelectRowAtIndexPath: implementation, you're asking the table view for a new cell to configure with dequeueReusableCellWithIdentifier:. You want to fetch the actual cell used for that indexPath so you can update it directly:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"Family Med Checked Box.jpg"];
}
You'll also need to update the data source for the table view somehow to record the fact that one of the rows is selected, and update tableView:cellForRowAtIndexPath: to set the checked image if the data source indicates that the row is selected. If you don't do those steps, the cell will appear unselected when it refreshes.
UITableViewDelegate has 3 methods to handle cell highlighting:
- tableView:shouldHighlightRowAtIndexPath:
- tableView:didHighlightRowAtIndexPath:
- tableView:didUnhighlightRowAtIndexPath:
Try like this :
- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
return YES;
}
- (void)tableView:(UITableView*)tableView didHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"Highlightimage"];
}
- (void)tableView:(UITableView*)tableView didUnhighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"image"];
}
You're trying to update the cell at the didSelectRowAtIndexPath function which returns void. So it will not update the tableview.
You should take the indexpath.row value from the diddSelectRowAtIndexPath and pass it to the cellForRowAtIndexPath. if they're matching, update the cell, it will work.
Also make sure you add a beginUpdate and endUpdate to reflect the table value reload
Related
When I put a check mark on a UITableViewCell, the cell I didn't put checkmark (that is not visible) become also checked.
I think that when the tableview reuses cell, something is wrong.
How do I fix it?
- (void)viewDidLoad
{
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, 200, 300)];
[myTableView registerClass:[AreaCellInShakeVC class] forCellReuseIdentifier:#"areaCell"];
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.allowsMultipleSelection = YES;
[self.view addSubview:myTableView];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = #"Cell";
CustomCell* cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
You need to have some logic in your cellForForIndexPath that sets the checkmark accordingly. This also means you need to store the status of the cell somewhere so you can check if it is ticked or not. So, set up an array, with an entry per row, then check that using your indexpath.row value to see if it should be checked or not. For example in cellForRowAtIndexPath have something like this
if ([[selectedRow objectAtIndex:indexPath.row] isEqualToString:#"Y"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
In your didSelectRowAtIndexPath you can then set the check accordingly by
if ([[selectedRow objectAtIndex:indexPath.row] isEqualToString:#"Y"])
{
[selectedRow replaceObjectAtIndex:indexPath.row withObject:#"N"];
}
else
{
[selectedRow replaceObjectAtIndex:indexPath.row withObject:#"Y"];
}
Then do a reloadData on your tableView to update the checkMarks. Hope this helps.
Set up checkmark in cellForRowAtIndexPath like
cell.accessoryType = UITableViewCellAccessoryNone;
After searching, I have an array with new data. I have confirmed this with print debugging
Then when it runs cellForRowAtIndexRow, my custom cell doesn't init. So I added an if statement to check if my cell is nil, and if it is reinitialize it. However, when the cell is returned, it doesn't update on my view controller.
When I print cell.cellLabel.text after my search, it is nil. This is most likely the reason why it isn't updating, even though I have confirmed that there is data in direntry
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UIImage *myImage = [UIImage imageNamed:#"Demo"];
static NSString *CellIdentifier = #"peopleCell";
PeopleCell *cell = (PeopleCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = (PeopleCell *)[[PeopleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
DirEntryItem *direntry;
if(tableView == self.searchDisplayController.searchResultsTableView){
direntry = [self.searchResults objectAtIndex:indexPath.row];
}else{
direntry = [self.peopleArray objectAtIndex:indexPath.row];
}
cell.name = direntry.fullname;
cell.phoneNum = direntry.phoneNum;
cell.email = direntry.email;
cell.admin = direntry.admin;
cell.cellLabel.text = direntry.fullname;
[cell.profile setImage:myImage];
return cell;
}
Is it possible to update the cell imageView in an iOS UITableView by tag? I appreciate other methods are available but I would like to use tags if possible.
I set the image by:
cell.imageView.image = [UIImage imageNamed:#"dinner.png"];
And in another method I can get the cell by (Tags are always unique):
NSInteger the_tag = ((UIView*)sender).tag;
UITableViewCell *updateCell = (UITableViewCell*)[tableView viewWithTag:the_tag];
Is it possible to update the cell with this method? This does not work:
updateCell.imageView.image = [UIImage imageNamed:#"lunch.png"];
EDIT:
On reflection, the tags are bad method to use. You can use (Which will give you the row and the section if you have a sectioned table):
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
NSLog(#"Tapped Row %#", indexPath);
UITableViewCell *cell = [self->tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"lunch.png"];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:indexPath.row];
UIImageView *imageView = cell.imageView;//CustomCell you have imageView as #propertyVariable..
imageView.image = [UIImage imageNamed:#"image.png"];
}
I hope it will be helpful for you...
try calling [updateCell setNeedsLayout] or [updateCell.imageView setNeedsLayout]
Thank you for all the answers. On reflection, I feel tags are a bad method to use. I have updated my original post with what I feel is a better solution.
- (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];
cell.tag = indexPath.row;
cell.imageView.image = [UIImage imageNamed:#"icon.png"];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int tag = 2;//this is same at table cell row number.
UITableViewCell *cell = (UITableViewCell*)[tableView viewWithTag:tag];
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}
In my iPhone app I have to show check-mark image on left side of the cell, first time there is no image on the cell, when user select the cell it will show the check mark image on right side of the cell, if user again select the same cell image should be hide.
First in your UITableView's Method
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Your code...
return cell;
}
and for show and hidden
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*) indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
Hope this will help you.
All the best !!!
if (cell.checkMarkImage.image == [UIImage imageNamed:#"checkMarkiPad.png"])
{
cell.checkMarkImage.image=[UIImage imageNamed:#""];
}
else{
cell.checkMarkImage.image = [UIImage imageNamed:#"checkMarkiPad.png"];
}
You can use the accessorytype method of UITableViewcell,which can make you mark "checkmark" on the right side of the cell. And you have to use Custom button on the left side of the tableview,on which when user clicks,the chek and uncheck images should be changed.
in cellForRowAtIndex method ->
{
// make a custom UIButton and set image on it.
UIButton *chk = [[UIButton alloc]initwithframe(0,0,30,30)];
[chk setBackgroundImage:[UIImage imagenamed:#"uncheck.png"]];
[chk addTarget:self action:#selector(YourbuttonAction:) forControlEvents:UIControlEventTouchUpInside]];
[cell addSubview:chk];
}
-(void)YourbuttonAction
{
// in this part ,you can change the buttons image on every click.
}
And if you still have problem,Have a look at following links ->
http://danielsaidi.wordpress.com/2012/08/14/checkable-uitableviewcell-with-custom-image/
http://pastebin.com/L99v2jBQ
I have a tableview that is made up of custom tableview cells which contain an imageview. In cellForRowAtIndexPath I set the image of each cell. I want the image to change when the cell is selected, so in didSelectRowAtIndexPath I get the cell and change the image, no problems there. However, when I scroll the table (read: table reloads the cells) the new image is no longer present. Also when the cell is no longer selected I want the image to switch back to the original.
I have tried doing the following in cellForRowAtIndexPath:
if(cell.isSelected){
cell.imageview.image = [UIImage ImageNamed: #"selected.png"];
}
else
cell.imageview.image = [UIImage ImageNamed: #"not selected.png"];
I have also tried using the BOOL values cell.highlighted or cell.selected to no avail.
Any thoughts are appreciated.
Try using a class variable selectedCellIndexPath of type NSIndexPath. In didSelectRow... you set it's value, and in the cellForRow... you write:
if([selectedCellIndexPath isEqual:indexPath]){
cell.imageview.image = [UIImage ImageNamed: #"selected.png"];
} else {
cell.imageview.image = [UIImage ImageNamed: #"not selected.png"];
}
Edit:
Or you could simply write:
if([indexPath isEqual:[tableView indexPathForSelectedCell]]){
cell.imageview.image = [UIImage ImageNamed: #"selected.png"];
} else {
cell.imageview.image = [UIImage ImageNamed: #"not selected.png"];
}
but the first solution is a little more efficient.
If you have an array with the Name of the Images it would be the easiest to change the name at the selected index in the array and reload the table... Like This:
myImageArray = [[NSMutableArray alloc] init];
[myImageArray addObject:#"name1.jpg"];
[myImageArray addObject:#"name2.jpg"];
// etc..
And the in the cellForRowAtIndexPath Method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
[cell.imageView setImage:[UIImage imageNamed:[myImageArray objecAtIndex:indexPath.row]]];
}
In the didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[myImageArray setObject:#"newName.jpg" atIndexedSubscript:indexPath.row];
[tableView reloadData];
}