Strange behavior after selecting cell by code - objective-c

I ran into some strange behaviour after I selected a cell using this code :
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomCell * cell = (CustomCell*)[_myTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:NO];
After this i couldn't select exactly this cell it just didn't respond and
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
was never called.
This wasn't called either:
-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
What i am doing wrong guys?

You need to call both methods manually because manually cell selection does not call delegate method.
[cell setSelected:YES animated:NO];
[self tableView:_myTableView didSelectRowAtIndexPath:indexPath];

According to Apple's documentation it is expected behavior that the methods you mention are never called after programmatically selecting a cell. More info is in this post.

Related

Min and Max button functionality in UITableVIew

I want to do min/max (expand/collapse) button functionality.Please see bellow image for more information.Is anybody know how to develop this functionality.I have some examples but those are not like exactly what i want.Please help me to solve this problem.
I've got a similar kinda requirement few days back. I have done this using custom tableview cell.
Firstly, you have to pass the indexpath of each row to the cell. For this i have taken a property in custom tableview cell.
i.e, #property(nonatomic,assign) NSIndexPath *indexPathOfCell;
Now, you have to pass the indexpath of row in datasource method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
And you have to write a delegate method in custom tableviewcell so that when the user clicks on the min/max button you can reload that cell through this delegate call.
-(void)reloadCellWithIndexPath:(NSIndexPath*)indexPath;
You have to set the view controller as delegate for the custom tableview cell in which you are populating the table. In that class you have to maintain an NSMutableArray.
NSMutableArray *selectedIndexPaths;
When ever the reloadCellWithIndexPath gets called you can simply do same.
-(void)reloadCellWithIndexPath:(NSIndexPath*)indexPath{
if(![selectedIndexPaths containsObject:indexPath]){
[selectedIndexPaths addObject:indexPath];
}else
[selectedIndexPaths removeObject:indexPath];
[tableView reloadData];
}
In heightForRowAtIndexPath method you can change the height for a particular row.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
BOOL cellModeChanged = NO;
for (NSIndexPath *indexpath in selectedIndexPaths) {
if(indexpath.section == indexPath.section && indexpath.row == indexPath.row)
cellModeChanged = YES;
}
if(cellModeChanged)
return 400.0;
else
return 100.0;
}

How to execute a code after an animation?

I have this simple code:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BGEditTextField * theTableViewCell = (BGEditTextField *) [self.tableView cellForRowAtIndexPath:indexPath];
theTableViewCell.displaytextField =true;
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//self.helpFinishUpdating.viewNotToCover = theTableViewCell;
}
Everything is perfect except that I want
self.helpFinishUpdating.viewNotToCover = theTableViewCell
to be executed after
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; is done.
How would I achieve that?
The reason why I want to do so is because self.helpFinishUpdating.viewNotToCover = theTableViewCell; is sensitive toward the frame location of the UITableViewCell. I want the UITableViewCell to reach the final position first before executing that.
UITableViewDelegate conforms to UIScrollViewDelegate, so you can just implement scrollViewDidEndDecelerating: or whatever fits your needs.

iOS set selected background to table row after reload data?

I have following issue: I have a UITableView.If you click on a cell, a button is added on cell. No Problem till now... but: After adding button i want the TableView (after its reload) to select the just edited (or added) TableCell.
I've tried this in cellForRowAtIndexPath method
if (bool) {
[cell setSelected:YES animated:YES];
}
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor colorWithRed:34/255.0 green:139/255.0 blue:34/255.0 alpha:1.0]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
return cell;
But it didn't work.
Any suggestion or sample code would be appreciated. Thanks .
For the standard selection you use the following method, described at UITableView reference docs
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
Your design might be improved if you use the custom cell storing information of selection count, that is much better than reloading the table and adding a lot of cell logic outside of the cell.
Why are you using
[tableView reloadData];
You can access the selected cell properties and controls without reloading the data using:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Here is some code that can help you:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.myButton setHidden:false];
}

Return NSIndexPath from custom cell ? (UITableView)

I need to get the NSIndexPath for a custom cell in a UITableView. Here's the problem: I send a NSNotification from my custom cell to my UITableViewController when editingDidBegin gets called from a UITextField in my custom cell. In my UITableViewController, I resize the UITableView when the UITextField began editing, and then want the table view to scroll to the Cell in which the UITextField is first responder. But I can't figure out how to return the indexPath of the cell where the UITextField is being edited. I have tried so many ways, but its still not working. One way is this: in my cstomCell class i select the row using [self setSelected:YES] and in my TV controller then if I NSLog the row of [self.tableV indexPathForSelectedRow] it always returns 0 even though its always not 0.
Just give the cell a value for its tag property. Then you can get that cell by calling this
UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];
then once you have the cell you can get the NSIndexPath like this
NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];
Since you have a UITextField in your custom cell you can place cell.textField.delegate = self; in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
data source method. That way you will not have to setup a NSNotification in the Custom Cell. Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row; and the cell like this cell.tag = indexPath.row;
Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
The above UITextField delegate method should get you the indexPath for the cell you have currently selected.
try this code..
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITableView *tableView;
UITableViewCell* superViewCell = [self returnSuperCellViewOfTextField:textField];
if(superViewCell)[tableView scrollToRowAtIndexPath:[tableView indexPathForCell:superViewCell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
-(id)returnSuperCellViewOfTextField:(id)viewToCheck
{
id superView = [viewToCheck superview];
if([viewToCheck isKindOfClass:[UITableViewCell class]] && superView)return superView;
else if(([viewToCheck respondsToSelector:#selector(superview)] && superView)) return [self returnSuperCellViewOfTextField:superView];
return nil;
}
Does this (your answer that you gave before below)still works if you have multiple texfields in each cell?:
UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];
then once you have the cell you can get the NSIndexPath like this
NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];
Since you have a UITextField in your custom cell you can place cell.textField.delegate = self; in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
data source method. Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row; and the cell like this cell.tag = indexPath.row;
Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
I have a tableview with 8 cells, and in each cell i have 6 texfields, and also the tableview is dynamic, so the user can insert more cells...but i´m having trouble in how to save the text the user enters in each texfield. Is it possible that with "cell.textField.tag = indexPath.row", the controller identify´s the texfield i´m in?
I like to tag my cells like so:
/* Create a tag based on the section and row of a cell(used to retrive the cell for the beginEditingNextCell method. */
-(NSInteger)tagForIndexPath:(NSIndexPath*)path {
return path.section * 1000 + path.row + 1;
}
The other way is to pass the beginEditing data using a custom delegate, and include a cell model, which you can then use to figure out your path depending on how you are storing data.
Check out my github project, it has the code to handle this case, as well as resizing for the keyboard and tabbing to the next editable cell: https://github.com/andrewzimmer906/XCell

How to hide UITableViewCellSelection after pressed?

I have a UITableView that in my cellForRowsAtIndexPath, I have this:
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
However, after the action occurs in my UINavigationController and I go back, it's still selected. Also, I have a UIAlertView that pops up for one of my actions. How can I hide it after the user has finished tapping it.
Thanks
Override the delegate method like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You can do this:
[yourTableView deselectRowAtIndexPath:[yourTableView indexPathForSelectedRow] animated:NO];