How to call tableView method directly? - ios

I have a method constructed like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//some table related stuff
}
However I cannot call this, so I basically copied and pasted the whole function and renamed as:
- (void)jumpCountry: (NSIndexPath *)indexPath {
//some table related stuff
}
and calling this method by using:
[self jumpCountry:countryIndex];
However my class looks ugly (and not preferred) because it has got the same two methods. So, how can I call the initial method directly(I know that it is assigned to a button which invokes that method). I am using iOS6.1. Basically, the reason why I want to directly call is I have another thread that listens notifications(from location services), once a notification is received, the table view should be changed. The notification itself already searches for NSIndexPath, so there won't be any problem with that.

You can just put
[self jumpCountry:countryIndex];
to your method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method a delegate method of table view from UITableViewDelegate protocol and it gets call when user select a row in UITableView. you should not call this method by your self.
instead of you can create your method and do whatever you want to do and call it in viewDidLoad or any method.

To call programatically use
[tabelView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
Here scrollIndexPath is the indexpath of row to be selected
For creating indexpath
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];

Related

Can not select specific UITableViewCell after reloading tableView

My UITableView shows answers for questions.
To switch questions I apply CATransition to tableView and call reloadData method.
In case if user did select one row, went to another question and then returned back to the first question, I want the chosen answer to be selected.
I call
[tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
in delegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
But when answers appear after reloading UITableView, there are no selected cells. If I move cell out of the screen and then move it back, it becomes selected.
you need to implement following UITableView delegate method to perform any action on selection of cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// [self doSomethingWithRowAtIndexPath:indexPath];
// write your action on cell here
}

Is it possible to create UITableViewCells inside a for/in loop?

I'm calling a function in which i retrieve a NSDictionary with data that needs to be displayed in tableviewCells. Is it possible to call the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
in another method?
It is not possible to call that one specific method inside another method. However, you can call [self.tableView reloadData] inside any methods. This call all the UITableView delegate methods which includes - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can explicitly call any methods of UITableViewDataSource and UITableViewDelegate, but should only be done in cases of dire need. In a project of mine, I get specific (subclassed) cells which draw elements not in the drawRect: method, but in a drawCell method I have defined, for performance gains (preventing offscreen rendering, etc).
Unless your cells have special drawing needs, (resizing subviews, etc) you should let the cellForRowAtIndexPath: method do this work for you.
Still, if you want to explicitly get cells, you may do with:
[yourTableView cellForRowAtIndexPath:indexPath];
This will return you your cell. Mind you, it will execute all relevant code inside the cellForRowAtIndexPath: including dequeueing, creation, and any other calls you have made in it.
indexPath passed here is an object of type NSIndexPath, and you may create one like this:
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:ROW_NUM inSection:SECTION_NUM];
Where, ROW_NUM is the is the row number inside the section, which is SECTION_NUM. For the first cell in the third section, the indexPath is:
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:2];//Since indices start at 0

UITableView not detecting the the accurate tapped row

I have just added a UITableView in to my storyboard and the suggested constraints to it like that.. Now i have written very simple code with some delegate and datasource methods implemented like this. . Now when i tap the first row it detects nothing and check the console in the below image, when i press the second row it tells me that the indexPath.row is 0.. can some body point out that what i am doing wrong here.
You are calling -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath which is called when you deselect row , instead call
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
It's because you are using didDeSelectRowAtIndexPath: instead of didSelectRowAtIndexPath: method.
You have to call "didSelectRowAtIndexPath" Delegate method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method only return which row we have selected.

What is the best way to implement one data source that services multiple table views in iOS

I have three UITableView's and they all need access to essentially the same data. The only difference is how each table view formats the data. What is the best way to implement this? I was thinking that if each table view had a tag of some sort, I could reference that from my data source when sending back the UITableViewCell's.
Each of the datasource calls passes in the UITableView that is requesting the information. Comparing this tableview to your three, you can determine which one it is and format the data differently. I would probably implement separate methods for each tableview and, pass the datasource method to the appropriate method. The other method would then create the customized cell creation and setup. eg for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == [self tableView1])
return [self tableView1:tableView cellForRowAtIndexPath:indexPath];
if (tableView == [self tableView2])
return [self tableView2:tableView cellForRowAtIndexPath:indexPath];
if (tableView == [self tableView3])
return [self tableView3:tableView cellForRowAtIndexPath:indexPath];
Assert0(NO, #"UITableView not recognized!");
return nil;
}
- (UITableViewCell *)tableView2:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do normal cell creation for tableView1 here
}
I don't know if there's any gotchas off-hand for running multiple UITableViews off of the same datasource, so tread lightly.
look into singleton design pattern - here is an example on how to implement for iOS.

Why this prepare segue is not working?

When i click the tableview i want to send the currentbook details to another viewcontroller "detailpage" and here its not sending any value?? why also it shows error?
error : unknown receiver "tableview"
Why are you creating two GMMDetailPage Objects? Also, you should most likely have self.tableView instead of just tableView. It would also be better to pass the tableView into the prepareForSegues:sender: method like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"bookItem" sender:tableView];
}

Resources