开发者

Calling cellForRowAtIndexPath through an instance

I was wondering if the following scenario is possible. I have a UITableViewController class which contains ten rows displaying some data. Now, I want to show the first five rows in one tableview and the last five in another. Since I don't want to fetch the data again I was hoping to create an instance of my original table class and call its cellForRowAtIndexPath:(NSIndexPath*)indexPath to return the table cells and add it to the other two table views. Is it possible to call a delegate method through an instance? If so, how can I create an NSIndexPath object p开发者_开发百科ointing to a particular row in any section of the table...


Thanks for the replies. I tried calling the cellForRowAtIndexPath:(NSIndexPath*)indexPath but it always shows the following warning

'RootViewController' may not respond to '-cellForRowAtIndexPath:'

Also the application crashes with the message

 ***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x4bad30'

Had initialized NSIndexPath as Andrei described. Guess its just not possible to call the function.


If you don't want to fetch again, why don't you just add your fetch results to an array?

This would have the advantage that populating your tableview would not require "live fetching" - fetching data as you need it may in some cases make scrolling a tableview slow or stuttering may occur.

However, you could also try to add your tableviewcells to one or more arrays to keep references to them while you pupulate your tableview for the first time.


I don't think you need anything that elaborate.
Have all tables using the same datasource. Choose which items to display in which table based on the tag value for that table element. Call reloadData on all tables when anything changes.


My guess is that using the same UITableViewCell in 2 or more tables would not work.
If you are reusing cells then it's very likely that those duplicated cells will eventually be refreshed with another content.
To protect against this, you would need to clone those cells, in which case it's not feasible to work on a UITableViewCell level.
You're better off storing the data (displayed by the cells) in a array (or other storing way) and share that data across multiple tables.

Plus, you'd have the extra ability to customize the appearance of those tables independently while sharing the same content.

To answer your last question, you can create a NSIndexPath to point to any cell. The first index is the sectio, and the second index is the row in that section. For example, to point to section 2, cell 5 you can write something like this:

NSIndexPath* myIndexPath = [[NSIndexPath indexPathWithIndex:2] indexPathByAddingIndex:5]; 

//Or, with another method:

NSIndexPath* myIndexPath = [NSIndexPath indexPathWithIndexes:indexes_array length:count];


Remember that cellForRowAtIndexPath: is not a method of UITableViewController. It is a method of UITableView. With this in mind you would actually call the method like so:

UITableViewCell *cell;
cell = [yourTableView cellForRowAtIndexPath: indexPath];

Doing so should return the cell at indexPath.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜