I have a UITableview in a popover. I cannot seem to get the deselection to animate or work at all. The checkmark stays there on the previous selected row (the checkmark is set from my Model class which is just an NSDictionary, where one value is #"YES", and all other values are #"NO." When I close the popover, and reopen it, the checkmark is correct, but I cannot seem to get it while the popover is open. Thoughts? Thanks.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// [cell setSelected:YES animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
deselectRowAtIndexPath does not remove the accessoryView of the UITableViewCell, it removes the highlighted (blue) background of the UITableViewCell. The following code sets the check mark of a cell:
cell.accessoryType = UITableViewCellAccessoryCheckmark;
To remove the checkmark from that cell, you have to do:
cell.accessoryType = UITableViewCellAccessoryNone;
Related
I am trying to change the selected UITableViewCell, but a little weird thing is happening here. I have placed the two images. Please take a look:
When I select the 'early' cell, it turns blue. It moves to the 'early blah blah' UIViewController. Then when I hit the 'Find' UIButton, it goes back to the first view.
It works, but the cell is still selected blue! So I wrote some code as below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_currentMethod isEqual: #"searchIt"]) {
if (indexPath.row == 0) {
BookMark2ViewController* bookMark2 = [[BookMark2ViewController alloc] init];
UITableViewCell* cell = [self tableView:_tableView
cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self.navigationController pushViewController:bookMark2 animated:YES];
}else if (indexPath.row == 1) {
BookMarkViewController* bookMark1 = [[BookMarkViewController alloc] init];
[self.navigationController pushViewController:bookMark1 animated:YES];
}
}
The two lines right in the middle of the code:
UITableViewCell* cell = [self tableView:_tableView
cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
These two lines didn't work. Does any one know how to fix this?
If you dont want the cells to show selection at all, place cell.selectionStyle = UITableViewCellSelectionStyleNone in your cellForRowAtIndexPath: method.
if you want the cell to be selected and then deselect, place
[tableView deselectRowAtIndexPath:indexPath animated:YES];
in your didSelectRowAtIndexPath:
hope this helps
UITableViewCell* cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
// Make sure your cell is not nil.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I had a such situation.
I have an application in which i am changing the cell background image when the user selects each row.Like this`
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:#"white_cell.png"]];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:#"white_cell_check.png"]];
if (indexPath != nil) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
`it is working fine.My problem is from the detailed view when i am poping back to the previous view Ie to the tableview controller the changed background image in the tableview stays like as.But i need it to be the normal one.Can anybody help me?
Just reload the table when your controller reappears, it will reload the data, and didSelectRowAtIndexPath will not get called, so your cell will have the initial background.
- (void)viewWillAppear:(animated){
[super viewWillAppear:animated];
[instanceToYourTableView reloadData];
}
In cellForRowAtIndexPath you should restore the cellBackground, and on viewWillAppear call reloadData
I want Checkmark in particular TableView Cell.
So I have used code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
newRow = [indexPath row];
oldRow = [lastIndexPath row];
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
else{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
lastIndexPath = indexPath;
}}
Its working in iPhone Simulator fine.
But while testing in iPhone device, it crashes the application.
Any solution for this..?
Thanks in advance.
The most likely cause of your crash is in your ivar, lastIndexPath. You're storing values in this without retaining them, so they may be released at any time. Try defining a property named lastIndexPath (retain for manual reference counting, strong for automatic reference counting). Then you can use self.lastIndexPath = indexPath or [self setLastIndexPath:indexPath].
Also, it's bad to forcibly change a cell contents like this. It's better to store the selected index, then reload the table data. For maximum efficiency, only update the changed cells. Have your cellForRowAtIndexPath: method switch the checkmarks on and off.
Simply write following code in "cellForRowAtIndexPath" method.
[[cell imageView] setImage:[UIImage imageNamed:#"tickmarkBlue.png"]];
[[cell imageView] setHidden:YES];
if(<your condition here>)
{
[[cell imageView] setHidden:NO];
}
In my UITableView, i need to get the event of the user, so when the cell is selected(checked), i put btn_checked.png and when it's unchecked, i put the btn_unchecked.png.
I guess, what i try to do should be in the didSelectRowAtIndexPath method, so this is my code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_unchecked.png"]];
theCell.accessoryView = imageView;
}
My snippet code doesn't work, so the btw_unchecked.png isn't placed when the row is checked, also, i need to get the event of checked/unchecked to place the right image to the right event.
Thanx in advance.
As an example, this code snippet will toggle the cell's checkmark when the cell is tapped. The pattern is to force a selective reload from the table's datasource when the delegate method is invoked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Dequeue the cell... then:
static NSString *CellIdentifier = #"tvcOEList";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = (cell.accessoryType == UITableViewCellAccessoryNone) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
You can use following code.
[cell.contentView addSubview:imageView];
i hope this will help you..
I'm trying to change the custom accessoryView of a uitableviewcell immediately after the user clicks on the cell. How would I do this?
For the record, I'm using Matt Gallagher' custom table view tutorial:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
Download link for source: http://projectswithlove.com/projects/EasyCustomTable.zip
EDIT
Tried this code, but it just changes the accessory AFTER touch-up. Also tried willSelectRowAtIndexPath with same result.
- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *indicatorImage = [UIImage imageNamed:#"indicatorSelected.png"];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
return indexPath;
}
EDIT
Problem solved by making the background image of the cell contain the accessories. so the accessory was 'faked' by making it part of the image
Perhaps you can try to reload the cell in willSelectRowAtIndexPath/didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *indicatorImage = [UIImage imageNamed:#"indicatorSelected.png"];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[[UIImageView alloc] initWithImage:indicatorImage] autorelease];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:NO];
}
Another recommendation:
didSelectRowAtIndexPath returns void not NSIndexPath.
You could change the accessoryView in the selection method as shown:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = newView;
}
This just grabs the current cell that was selected and then changes the accessory view of it. Your view object would go where newView is.
Use the highlightedImage property of UIImageView:
UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage];
arrowView.highlightedImage = selectedImage;
cell.accessoryView = arrowView;
[arrowView release];