Hello I have a problem
I implement this method
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: NSIndexPath *)indexPath
{
NSLog(#"Row=%i",indexPath.row);
}
If I run the app and tab on the DisclosureButton of the second row the output is "0". If I tab on the second row (not the DisclosureButton) the output is "1" (as aspected). The bug is only one the second row, all other rows work fine.
How can I fix this problem?
thanks
Related
In my application I have an UITableView with dynamic number of rows. Once user click on a cell, the application navigates to another UIView and user can click the back button to go to the UITableView. When going back I wanted to highlight the selected cell and scroll the cell to the center. This is how I achieve it
-(void) tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (!isScrollerSet && self.selectedRowNo!=-1) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.selectedRowNo
inSection:0];
[self.tableViewP selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionMiddle];
isScrollerSet = YES;
}
}
When the user comes back this method will be call when the rows are displayed again. I set the correct row number in the didSelectRowAtIndexPath: method.
When the UITableView has less numbers of rows this works fine. Lets say UITableVIew have 100 rows and user clicked on 90th row. When he comes back application crashed when calling the didSelectRowAtIndexPath: method. Exception is as follows
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds
for empty array'
Then I move this code to viewDidAppear:. Now it woks with with any number of rows. I cant figure out why this happen. Can some one explain what was the reason?
Assuming that you do not have defective code it is possible that:
-(void) tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
is called before your Datasource is initialized. For example, if your UITableView is instantiated in the UIViewController's viewDidLoad function, and then you initialize your Datasource afterwards then upon reloading the UITableView you will have a nil value.
Whereas viewDidAppear occurs later in the runtime than viewDidLoad so at that point your Datasource is valid and you're able to reference that index in the NSArray. If this is the cause of your exception then you would be able to solve it by doing your Datasource initializing at the very beginning of viewDidLoad.
This is my best guess based on the information you provided. You may want to give more detail in your question.
tableView willDisplayCell
method is called before your data is loaded.so deferentially it will show an error because it is not getting any index position because of no data in tableview
So the solution is that you can set an integer or any other variable and check it inside cellforRowAtindexpath. when you return back..(Better to store value in UserDafault)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if(indexpath.row==<your value>)
{
// your code.
}
....
}
I have 2 UITableViews. When I click the UIButton outside of either UITableView I'm trying to determine which cells are selected in each tableview.
Anyone have a good way of doing this? Would I loop over the cells to determine which cell is currently selected? Or is there already a method that can tell me?
Thanks a bunch!
You can use this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
Take a look at UITableViewDelegate Protocol Reference
UITableViewDelege Reference
EDIT: When the user select a row in one of your tables you can get it using this method and a couple of variables for the table and the cell and then you can pass what you got as soon as the user tap on your button.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
thisIsMyTable = tableView;
thisIsTheSelectedRow = indexPath.row;
}
- (void)buttonTapped {
// Do what you need
}
I wonder if someone would be so kind as to help me out with a query I have regarding deleting rows from a UITableView. I have implemented tableView:commitEditingStyle:forRowAtIndexPath: and now get the [DELETE] button appearing when I swipe on a cell, my question is which method do I need to use to catch a user pressing that button (i.e. [DELETE]).
Also when I do delete a row, can I delete the row from my dataSource and refresh the table. OR should I delete the row from the dataSource and then call some other method on the tableView?
This is what I was after, the callback for the swipe and the callback when the user selects [DELETE].
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Swipe ...");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Delete Pressed.");
}
Delete from the data source and call reloadData on the table. Or do you have like a million rows in the table?
I have UITableView and I want to load more data when user tries to scroll up at the bottom row of the tableview just like iPhone email application...What is the best way for this?
Implement the Delegate for UIScrollView and detect at which point where you want to load more data.
Or You can put the update code in the "cellForRowAtIndexPath:" method, here detect the row, say 100 and append the data, and remove the data in row <100 , you will have to check for dataSource's count before updating.
Append new contents to the Data Source (Array) and do this:
[self.tableView reloadData];
I tried adding this in UITableViewDelegate method "willDisplayCell"
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == floatRows - 1) {
floatRows += 10;
[tableView reloadData];
}
}
It is keep loading another 10 for every scroll to bottom.
Not sure, it will work for you.
I need a small help. In my project there is a tab bar. On one of the tab bar items there is a navigation controller, on the view there is a table view.
For TableViewController there are the usual codes:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// here the DetailViewController is activated
}
The construction of DetailViewController is similar to the TableViewController. The data related to the selected row of TableView will be the key data for the SQL query, the result of which is then stored in an array. The only difference is that I use this:
viewWillAppear: includes and SQL query, then loads the result into the array sqlQueryResult.
- (void)viewWillDisappear:(BOOL)animated {
while( [sqlQueryResult count] >0 )
[sqlQueryResult removeObjectAtIndex:0];
}
This works the very first time when the table DetailView is shown. However, if we go back to TableView and select a different row, DetailView appears but the followings will not run again (as they did already): numberOfSectionsInTableView: or, tableView:(UITableView *)tableView numberOfRowsInSection: . I read in the docs that there are methods that are called only once - however I haven't seen such remark for these ones.
How can I "reset" the TableView? Or what methods should I call to set the number of its rows/sections?
What you're looking for I believe is:
[self.tableView reloadData];
You basically need to send a 'reloadData' message to your tableView. It will then call your methods again to refresh its contents.