How to set the selected row of a tableView [duplicate] - iphone

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];
}

Related

How to know the section number on button click of Tableview cell in a UITableView? [duplicate]

This question already has answers here:
How to know the UITableview row number
(10 answers)
Closed 9 years ago.
I have a UITableView for which I have created a custom UITableViewCell. Each row in tableview has a button. I want to know the section number on click of a button, so that I would know that from which section button has been clicked. I have already tried few things found on stack but nothing is working.
UIButton *b = sender;
NSIndexPath *path = [NSIndexPath indexPathForRow:b.tag inSection:0];
NSLog(#"Row %d - Section : %d", path.row, path.section);
Don't know what you've tried, but i might do something like this. Doing some pseudocode from memory, here.
- (void)buttonClicked:(id)sender {
CGPoint buttonOrigin = [sender frame].origin;
// this converts the coordinate system of the origin from the button's superview to the table view's coordinate system.
CGPoint originInTableView = [self.tableView convertPoint:buttonOrigin fromView:[sender superview];
// gets the row corresponding to the converted point
NSIndexPath rowIndexPath = [self.tableView indexPathForRowAtPoint:originInTableView];
NSInteger section = [rowIndexPath section];
}
If I'm thinking clearly, this gives you flexibility in case the button's not directly inside the UITableView cell. Say, if you've nested inside some intermediary view.
Sadly, there doesn't seem to be an iOS equivalent of NSTableView's rowForView:
Create a handler for button click and add it in tableView:cellForRowAtIndexPath: method
- (void)buttonPressed:(UIButton *)button{
UITableViewCell *cell = button.superView.superView;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//Now you have indexPath of the cell
//do your stuff here
}
When you create your custom UITableViewCell in cellForRowAtIndexPath you should pass it its section as a parameter. It could look like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (!cell)
{
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier" section:indexPath.section] autorelease];
}
return cell;
}
Now your cell knows its section and you can use it when performing click method in MyCustomCell class
Try this,
First assign section as a tag to button also add target on button in cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell.btnSample setTag:indexPath.section];
[cell.btnSample addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
...
}
Get Section as tag from sender of IBAction you defined (buttonClicked here).
-(IBAction)buttonClicked:(id)sender
{
NSLog(#"Section: %d",[sender tag]);
}

Highlight table view cell on click, not on release

I have a table view with a list of items. When I click on one of those items I want the background to be highlighted. I have that code working but the colour changes on release, not on the click itself. How can I highlight when the user taps on the cell and not when he/she releases it?
Here is my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Navigation logic may go here. Create and push another view controller.
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.backgroundView=v;
cell.title.textColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor1"]];
}
I want it to happen like a button. Is there an onClick method for a UITableView or a UITableViewCell besides didSelectRowAtIndexPath?
EDIT
Here is my code from cellForRowAtIndexPath
-(void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView=v;
}
At the time of creating cell, in cellForRowAtIndexPath, write below code:
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor = [self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView = v;
No need to write anything in didSelectRow method.
You can consider this as option
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView selectRowAtIndexPath:indexPath.row animated:YES scrollPosition:UITableViewScrollPositionTop];
}

Trying to load a picture in a cell when returning from push segue

I have a table view with a custom cell. My cell has an image on the left, and the image is a black png that I have switch to a white png when it is selected. The cell also has a push segue attached. My problem is when the back button is pressed in the destination view and you return to the table view, the icon is still white, so it looks like it disappeared. I need to load the black icon back in. I can't use didDeselectRowAtIndexPath, because the view is unloaded before it has an opportunity for this to kick in. Is there another method that's out there that I just don't know about? This is my current code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *array = [self.listArray objectAtIndex:indexPath.section];
StartRow *rowobject = [array objectAtIndex:indexPath.row];
StartCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.mainIcon.image = rowobject.iconWhite;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *array = [self.listArray objectAtIndex:indexPath.section];
StartRow *rowobject = [array objectAtIndex:indexPath.row];
StartCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.mainIcon.image = rowobject.icon;
}
You actually don't want to reloadData each time the table view about to appear. Instead you want to find the index path for the currently selected cell and then use this knowledge to hunt down said cell.
After you've found the cell that's selected, you simply swap out the image.
in viewwillappear write this
[tableview reloadData];

UITableView lose selected state iOS

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];

How do you change the textLabel when UITableViewCell is selected?

I want to change the textLabel and detailTextLabel of a cell when it has been selected.
I've tried the following, but no change occurs:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MyAppDelegate *appDelegate = (MyPhoneAppDelegate*)[[UIApplication sharedApplication] delegate];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = #"xxxxx";
cell.textLabel.text = #"zzzzz";
[tableView reloadData];
}
I agree, reloading the table view will actually dump and reload/display all the cells using tableView:cellForRowAtIndexPath: and use the original data, not the updated #"xxxxx" and #"yyyyy" in your tableView:didSelectRowAtIndexPath: method.
In a little test project I was able to change the labels upon selection with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = #"it was tapped";
}
You should not be trying to reload the table while a cell is selected. Instead, try
[cell setNeedsLayout]
after you make the above changes to the labels.
Also, is there a reason you're making a reference to the app delegate in the method?
Try to reload the cell you selected (described by indexPath) :
[yourTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
Create a New iPad Project (Split View) and Now go through the Classes->Files. The easiest way's been given there. The XCode's Generated Codes.
Sample Code Lines :-
cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
You can use them in cellForRowAtIndexPath ||&& didSelectRowAtIndexPath ..
Not sure what you're trying to do with the delegate but you should try calling the tableView already instantiated; i.e. call
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
Maybe I'm not clear
What I'm saying is that you are instantiating a new empty table view
UITableViewCell *cell = [**tableView** cellForRowAtIndexPath: indexPath]; //cell has nothing it is new.
consider replacing to call the old
UITableViewCell *cell = [**self.tableView** cellForRowAtIndexPath: indexPath]; //now you have one that has a textField already in it
Did you try to refresh only the selected cell instead of reloading the whole table ?
[cell setNeedsDisplay];
instead of
[tableView reloadData];
This will have better performance and I'm not but I suppose that selection is lost if you refresh the whole table (this may be the reason why you don't see any change in the end)....