I have a UITableView and have it so when you press the first cell, it takes you to a new view (new xib). When you press the second cell, you go to another view and so on. When you go to any of those new views, and come back to the tableview view, the cell you just pressed is still selected (its highlighted blue). What is the code to fix this?
In your tableView datasource delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Inside viewWillAppear of your tableView whose cell you wish to remove selection from:
UITableViewCell *cell = (UITableViewCell *)[myTableView cellForRowAtIndexPath:lastSelected];
[cell setSelected:NO];
Where lastSelected can be a global var of type NSIndexPath storing indexPath from the didSelectRowAtIndexPath of the above UITableView
It's quite easy..
In the same method that you push the new view you should add the line:
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated];
Related
This question already has answers here:
Top cell name changes when changing any cell name
(2 answers)
Closed 9 years ago.
I have a UITableView and I add cells with a button on the UIViewController.
If the cells are tapped, you are brought to a new viewController which has a picker.
How can I set the the selected cell's text as title for next viewController?
IF YOU WANT TO SEND SELECTED ROW's TITLE TO NEXT VIEW CONTROLLER (push)
Create property (say title) in your new view controller's .h file to hold title of selected row and synthesize title property in your new view controller's .m file.
update didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YourNewViewController *vc= [[YourNewViewController
alloc]initWithNibName:#"YourNewViewControllerNIB" bundle:nil];
vc.title=[[titleArray objectAtIndexPath]indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
now you have selected row's title in your new view controller.
for setting this title as navigation bar title
write this code to viewDidLoad of your new view controller.
self.navigationItem.title=title.
IF YOU WANT TO SEND SELECTED ROW's TITLE TO PREVIOUS VIEW CONTROLLER (POP)
for this you have to use delegate pattern and you have to make custom protocols.
follow this. -
Passing Data between View Controllers
// This is how to get selected cell
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];
cell.textLabel.text = #"Some title";
In the didSelectRowAtIndexpath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *NewTitle = #"Add your new title here";
Write this code in either button's method OR your picker's method
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[tableView indexPathForSelectedRow]];
cell.textLabel.text = self.MyButtonName.titleLabel.text;
[self.tableView reloadData];
Are you looking for the selected row?
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
[cell.textLabel setText:#"Your Title"];
[_tableView reloadData];
}
When we select UITableViewCell than - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath called.
But which method will be called if we hold the UITebleViewCell. Guys my problem is that I have created a tableview which contain large cell and in that cell I am setting various view an view's background color.
When I select the cell, the background color of my cell will be gone. I have solved this problem by setting view background color again in didSelectRowAtIndexPath method like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UIView *vLineview=[selectedCell viewWithTag:1];
vLineview.backgroundColor = [UIColor colorWithRed:(89/255.0) green:(89/255.0) blue:(89/255.0) alpha:1];
}
This done the trick and my view background color will displayed but when I hold the UITableViewCell than it will gone again.
How can I solve this? Do I have to and gesture recognizer to detect long touch and implement my view background method in it? Or there is any other method available for that.
Try with set cell selection style none like this in cellForRowAtIndexPath
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
You can subclass UITableViewCell and override this method:
- (void) setSelected: (BOOL) selected
animated: (BOOL) animated
{
[super setSelected: selected
animated: animated];
// Configure the view for the selected state
}
Is this an issue with the UITableViewCell remaining selected after you tap on the row?
If row selection is not required, make sure you call deselectRowAtIndexPath:animated at the end of your tableView:didSelectRowAtIndexPath method.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Pleasese see method named -(void)beginUpdates
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.
for more detail visit apple documentation in below link
UITableView Class Refrence
Also try [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.
The cell in this iPhone TableView definitely has a Detail-Disclosure-Button.
When I tap it... shouldn't this code give me access to the button?
Instead detailButton is always just null.
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *detailButton = (UIButton *)[aCell accessoryView];
}
are you over-riding the cell accessory button in 'cellForRowAtIndexPath' by any chance?
accessoryView is provided for your own customization, to override accessoryType. The table view cell will internally handle whatever you assign to accessoryType without affecting accessoryView.
I want to add the content of the cell of tableview in other view when i click on the tableview cell , how can i pass the values of the cell to other view.
If you want use the text of a cellview for example you can do that in your UITableViewDelegate (or your UITableViewController):
// Tells the delegate that the specified row is now deselected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *contentCell = [targetCell textLabel];
// Do something with the contentCell
}
In your table view delegate (by default, it's the table view controller) implement the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Use indexPath as you did in -tableView:cellForRowAtIndexPath: to get the values you need. To avoid unexpected behavior you should extract the values from your model rather than the cell.