Trying to change cell content doesn't work - ios

I'm trying to change the background color of a cell after the user enters something in a textfield. I've tried the following code but nothing happens.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:2];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
Is there anything I don't see?

The only thing I can think of is that you have your section/row number wrong, i.e.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:2];
I have two sections in my tableView and it works as long as refer to section 2 as section 1, as section and row numbers start at 0. Apologies in advance if this sounds too obvious, but I do it all the time... :-)

Related

How to get UITableviewcell value on click on button?

I want to get UITableviewCell value on click of update button. When user press on update button then i want to fetch first cell(name) and second cell(oil cost) value. Please help me.
:(
Try below code,
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
UITableViewCell *cell1 = [self.tableview cellForRowAtIndexPath:indexPath];
NSString* name = cell1.textLabel.text;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
UITableViewCell *cell2 = [self.tableview cellForRowAtIndexPath:indexPath];
NSString* oilcost = cell2.textLabel.text;

How can I get a uitableViewCell by indexPath.row or only row number instead of indexpath?

we can get the UITableViewCell from indexPath easily like this:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
But i only have the row number not the index path, i am using this code to get the cell .
NSArray *visible = [self.tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:MyRowNumber];
UITableViewCell *tablecell = [self.tableView cellForRowAtIndexPath:indexpath];
Tell me whether its a good approarch and if not which one is.
You can create a new NSIndexPath with the row number (assuming your table has only one section).
NSIndexPath *indexPath= [NSIndexPath indexPathForRow:yourRowNumber inSection:0];

Callback for cellForRowAtIndexPath?

My Project:
I have an UITableView in My project. Each and every UITableViewCell have one UITextView as a subview. When user tap on 'Add' button, I need to add one more UITableViewCell with UITextView to UITableView and I have to set focus into the added UITextView.
What I've Tried:
Once user tap on add button, I will update the numberOfRowsInSection of UITableView and subsequently I'll call [UITableView reloadData] to invoke [UITableView cellForRowAtIndexPath]. Here, I'll append the UITextView as subview of UITableViewCell.It Works fine until this point.
My problem:
I need to set focus into the UITextView Once after the [UITableView cellForRowAtIndexPath] get called. When I called the set focus method for UITextView after [UITableView reloadData], It is not working. I want to know, Is there any callback method for [UITableView cellForRowAtIndexPath]?
Thanks for your answers.
Try this,
In Add button action
//Assuming only 1 section, if you have more section find sections by `numberOfSections` method
NSInteger totalRows = [self.tableView numberOfRowsInSection:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:totalRows inSection:0];
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
//If you have custom cell
CustomCell *cell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.textView becomeFirstResponder];
//If you are not using custom cell, then set tag to textView and
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//UITextView *textView = (UITextView *)[cell viewWithTag:kTextViewTag];
//[textView becomeFirstResponder];
after you have called your reloadData method you can do something like this:
To get a reference to the last row in the last section…
// First figure out how many sections there are
NSInteger lastSectionIndex = [self.myTableView numberOfSections] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.myTableView numberOfRowsInSection:lastSectionIndex] - 1;
// Now just construct the index path
NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
//Grab a reference of your cell from the last row
MyCustomCell *myCC = [self.myTableView cellForRowAtIndexPath:pathToLastRow];
//Use this cell reference to set focus to your UITextView
[myCC.myTextView setFirstResponder:YES];

Add cells to uitableView Like Mail.app

I want to add cells into UItableView like in mail.app on ipad. In my cells I have UITextFields, and I have change this method to set focus
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
NSLog(#"%d",newIndexPath.row);
UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row];
int l_cellNum = field.tag;
if(l_cellNum == 1 && isAddedNewRow == FALSE){
isAddedNewRow = TRUE;
[cellsArray insertObject:#"CC" atIndex:1];
[cellsArray insertObject:#"BCC" atIndex:2];
CGRect tvbounds = [tblView bounds];
[tblView setBounds:CGRectMake(tvbounds.origin.x,
tvbounds.origin.y,
tvbounds.size.width,
tvbounds.size.height)];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *path2 = [NSIndexPath indexPathForRow:2 inSection:0];
NSArray *indexArray = [NSArray arrayWithObjects:path1,path2,nil];
[tblView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
[tblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[field becomeFirstResponder];
}
But when I click to one of the cell i add 2 cells (like mail.app) but my tags for textFields do not updates, and I cant to set focus on it =(. If I call reloaddata method it calls -cellForRowAtIndexPath with this new cells at the beggining and after for all cells, and it will not work too.
It sounds like what you're looking for is a built-in style of cells called UITableViewStyleSubtitle, where you can customize the subtitle just like the iPad Mail.app. Take a look at this page in the iOS documentation. Then, you can set the subtitle of the UITableViewCell to the text that you need.
Additionally, the iOS doc has more information about customizing table view cells.

change color of UITableViewCell after reenabled userinteraction

in my app, i've some menupoints, that are disabled for userinteraction.
cell.userInteractionEnabled = FALSE;
after the login, i want to reenable some of these cells.
this snippet is working half the way:
NSIndexPath *editUsersPath = [NSIndexPath indexPathForRow:0 inSection:1];
[self.tableView cellForRowAtIndexPath:importPath].userInteractionEnabled = YES;
[self.tableView cellForRowAtIndexPath:importPath].accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self.tableView cellForRowAtIndexPath:importPath].textLabel.textColor = [UIColor blackColor];
but the last line of code is not working - the text is gray as before.
Are you doing any cell redrawing after the changes? Perhaps you can try calling setNeedsLayout to redraw the cell.

Resources