reading cell value from tableview - iphone

I am new in objective-c i want ur help
my question is that i have tableview with 10 cell each cell is field with some data that are come from tabase later i change some of the cell value from table view and now i want to read this all cell value from table view after table view reload with new value then how can i do
how can i stored it in new array
i am trying to do something like this it is correct or not if not then how can i do
-(void)viewWillAppear:(BOOL)animated // after new value will insert
{
[tableView reloadData];
NSArray *visibleRowsArr = [tableView indexPathsForVisibleRows];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[visibleRowsArr objectAtIndex:0]];
}

in viewwillappear
[tableview reloadData] should be at the end of viewwillappear that is before }

It's so easy, you can try use the same array from which you are reloading the whole table.
Because after all that array would have been updated so there is no need to read the records from TableViewCells but try it using with Array.
like :
int AtOurWish =5;
-(void)viewWillAppear:(BOOL)animated // after new value will insert
{
[super viewWillAppear:animated];
[self.YourArray objectAtIndex:AtOurWish];//Index At which you want to access the array
[tableView reloadData];
NSArray *visibleRowsArr = [tableView indexPathsForVisibleRows];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[visibleRowsArr objectAtIndex:0]];
}
Otherwise you edit you question since it's not much clear!!!

Related

Unnatural jerk with UITableView whe cell height is changed dynamically

Here is what I want in my app. Shown below are two screenshots of the iPhone app Store:
I basically need a "Read More" feature just like it is used in the app store (See the "Description" section in the two images above). I am assuming that each section here (Description, What's New, Information etc.) is a table view cell. And the text inside is a UILabel or UITextField.
Here is what I have tried so far to add this feature:
NSString *initialText = #"Something which is not a complete text and created just as an example to...";
NSString *finalText = #"Something which is not a complete text and created just as an example to illustrate my problem here with tableviews and cel heights. bla bla bla";
NSInteger isReadMoreTapped = 0;
My cellForRowAtIndexPath function:
// Other cell initialisations
if(isReadMoreTapped==0)
cell.label.text = initialText;
else
cell.label.text = finalText;
return cell;
My heightForRowAtIndexPath function:
// Other cell heights determined dynamically
if(isReadMoreTapped==0){
cell.label.text = initialText;
cellHeight = //Some cell height x which is determined dynamically based on the font, width etc. of the label text
}
else{
cell.label.text = finalText;
cellHeight = //Some height greater than x determined dynamically
}
return cellHeight;
Finally my IBAction readMoreTapped method which is called when the More button is tapped:
isReadMoreTapped = 1;
[self.tableView beginUpdates];
[self.tableView endUpdates];
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:2 inSection:0]; // I need to reload only the third row, so not reloading the entire table but only the required one
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
After doing all this, I do get the required functionality. The new height of that particular cell is calculated and the new text loaded into it. But there is a very unnatural jerk on the TableView which results in a bad User experience. That is not the case with the app store More button though. There is no unnatural jerk in its case. The TableView remains at its place, only the changed cell has its size increased with the text appearing smoothly.
How can I achieve the smoothness as done in the iPhone app store More button?
Your problem might come from reloading the row. You want to try to configure the cell properties directly. I usually use a dedicated method to configure my cell content so I don't have to reload rows.
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(isReadMoreTapped==0)
cell.label.text = initialText;
else
cell.label.text = finalText;
// all other cell configuration goes here
}
this method is called from the cellForRowAtIndexPath method and it will configure the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
and you would call this method directly to avoid reloading:
isReadMoreTapped = 1;
[self.tableView beginUpdates];
[self.tableView endUpdates];
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:2 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:rowToReload];
[self configureCell:cell forRowAtIndexPath:rowToReload];
Please try the following changes to your code, I think it will fix your problem.
no need to set cell.label.text in heightForRowAtIndexPath; Please remove them.
in the readMoreTapped, update table is enough:
isReadMoreTapped = 1;
[self.tableView beginUpdates];
[self.tableView endUpdates];
Either remove the calls to:
[self.tableView beginUpdates];
[self.tableView endUpdates];
Or change to ensure that your reloading code is between them. I would remove them as a single row reload is handled well with the method you use:
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
You just need to specify a row animation like fade.
Okay, I finally solved the problem with the help of Matthias's answer (the accepted answer) and my own optimisations. One thing that definitely should be done is to create a dedicated function like configureCell: forRowAtIndexPath: to directly configure cell properties (see Mathias's answer). Everything remains the same with Matthias's answer except:
Before, I was calculating the heights of each cell everytime the heightForRowAtIndexPath function was called without caching(saving) them anywhere and hence when [self.tableView beginUpdates]; and [self.tableView endUpdates]; were called each cell height was calculated again. Instead, what you have to do is to save these cell heights in an array/dictionary so that whenever the More button is tapped, you calculate the height of only the cell that was changed. For the other cells, just query the array/dictionary and return the saved cell height. This should solve any problems with the tableView scroll after the cell height update. If anyone else still face a similar issue as this, please comment on this post. I would be happy to help

Referencing a UITableViewCell that gets created in cellForRowAtIndexPath

I have a button on my TableView header that is an empty box, and when pressed, is a checkmark. I have that same button on the cells for that header. So I basically want to perform an action on each cell that is in that section. I'm using the Animating TableView from WWDC 2010 as my example. The header object has an array of integers that keep track of how many rows are in its section.
I'm not really sure from here, how I can get the custom UITableViewCell to perform my action on. Any thoughts? Thanks.
So far I have something like this:
- (void)selectAllCheckmarksInSectionHeaderView:(SectionHeaderView *)sectionHeaderView forSection:(NSInteger)section {
SectionInfo *sectionInfo = [self.SectionInfoArray objectAtIndex:section];
int totalRows = [[sectionInfo rowHeights] count];
for (int row = 0; row < totalRows; row++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:(NSUInteger)row inSection:section];
OrderTableViewCell *cell = (OrderTableViewCell *)[_orderTableView cellForRowAtIndexPath:path];
cell.CheckmarkButton.selected = YES;
}
}
However my OrderTableViewCell is nil.
To reference a button that is a located in a section header, you'll have to assign it to an instance variable to keep a reference to it. To obtain references to all of the cells in a section and update them, you'll want to try something like this...
CGRect sectionRect = [self.tableView rectForSection:0]; // Rect for first section
NSArray *indexPathsInSection = [self.tableView indexPathsForRowsInRect:sectionRect];
for (NSIndexPath *indexPath in indexPathsInSection)
{
// Obtain the cell at each index path and update its accessory state
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Of course this is an overly simplistic example. A more flexible approach would be to update some model object when a section is "selected" and reload the table. Then in your -tableView:cellForRowAtIndexPath: you would determine whether a cell in a section should be checkmarked based off the model object for the whole section.

tableView's insertRowsAtIndexPath doesn't call tableView's cellForRowAtIndexPath

I have a UITableViewController with an Edit button. When this is clicked, the user can delete or add rows to the UITableView. Deleting rows works fine. But when I want to add a new row, I get a short animation of an empty cell coming from the left and when the animation is over, the table view looks exactly as it looked before the method was called. When I add a new cell, I add a new object to the data array that feeds my table view and then add the cell. That data array gets updated. So if I call [tableView reloadData] after I have added the new cell, I get to see the new cell but without any animation. And I really would like to have the animation as well.
I have a working example of the same thing. I realized that after tableView:commitEditingStyle:forRowAtIndexPath: gets called, the table view's data source method tableView:cellForRowAtIndexPath: gets automatically called. In my case, it doesn't. I guess that's the reason why I'm not seeing the new cell. Any ideas why this is happening?
This is my `tableView:commitEditingStyle:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
[self.tableData removeObjectAtIndex:[indexPath row]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tableData forKey:self.key];
[defaults synchronize];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *textField = (UITextField *)[[cell contentView] viewWithTag:kTextFieldTag];
NSString *textFieldText = [textField text];
if (textFieldText != nil)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
NSString *string = [[NSString alloc] initWithString:textFieldText];
[self.tableData addObject:string];
[string release];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tableData forKey:self.key];
[defaults synchronize];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// If reloadData is being called, the new cell is displayed but without the animation
// [tableView reloadData];
}
else
{
// Display alert view
// Code for displaying an AlertView
}
}
}
Try wrapping your insert call with [tableView beginUpdates] [tableView endUpdates]. From the docs:
If you do not make the insertion, deletion, and selection calls
inside this block, table attributes such as row count might become
invalid.
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
I'm having this same issue. From what I can tell, the UITableView only calls cellForRowAtIndexPath if it thinks the cell being changed is visible. Since you are adding a new cell I'm guessing the table does not think it's visible and therefore does not call the method. Jaminguys solution works for me, but it sounds a bit like a bug in UIKit.
This method does work if you are inserting cells in the middle of the table view, incidentally.

Reload specific UITableView cell in iOS

I have a UITableView. I want to update the table data based on selection made. Right now I use [mytable reloadData]; I want to know if there is any way where I can just update the particular cell which is selected. Can I modify by using NSIndexPath or others? Any suggestions?
Thanks.
For iOS 3.0 and above, you just have to call :
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
To reload row 3 of section 2 and row 4 of section 3 for example, you'll have to do this :
// Build the two index paths
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:3 inSection:2];
NSIndexPath* indexPath2 = [NSIndexPath indexPathForRow:4 inSection:3];
// Add them in an index path array
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, indexPath2, nil];
// Launch reload for the two index path
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
You can also get a reference to the UITableViewCell object and change its labels, etc.
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = #"Hey, I've changed!";
I think you're looking for this method of UITableView:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

UITableView Detect Selected Cell?

I have multiple UITableViews in my app, is there a method of detecting which cell/row the user has selected in that column?
Also is it possible to programatically deselect a cell/row?
Thanks.
Get currently selected index path for a table:
NSIndexPath *path = [tableView indexPathForSelectedRow];
Deselect currently selected row:
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
These are all easily found in the documentation for UITableView. Perhaps you should look there first next time.
Here's even a link: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
NSIndexPath *path = [tableView indexPathForSelectedRow];
The above line will throw EXC BAD ACCESS if no sell is selected so keep track of your selected cell with NSIndexPath instance variable and only deselect when it is actually selected with isSelected property of cell.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
if(cell.isSelected) {
[tableView deselectRowAtIndexPath:someIndexPath animated:YES];
}