Saturday, September 28, 2013

Two tableview cell methods to obtain reuse difference?

tableView: cellForRowAtIndexPath: There are two ways to obtain reuse cell method

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

And

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath: indexPath]

I ask them what's the difference?

And when I use UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath: indexPath]

When

Why does an error

reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Reply:
- (Id) dequeueReusableCellWithIdentifier: (NSString *) identifier;
- (Id) dequeueReusableCellWithIdentifier: (NSString *) identifier forIndexPath: (NSIndexPath *) indexPath
NS_AVAILABLE_IOS (6_0); / / newer

Difference here

Reply:
quote 1 floor jiangyuaiqing reply:
- (id) dequeueReusableCellWithIdentifier: (NSString *) identifier;
- (Id) dequeueReusableCellWithIdentifier: (NSString *) identifier forIndexPath: (NSIndexPath *) indexPath
NS_AVAILABLE_IOS (6 ......


ios6 new method?

The key is how to use? Why an error?
Reply:
1 This method is not running in SDK5.0 up.
2 If you need to use this method, you must use matching methods to be used together, supporting the following two methods:
 
/ / Beginning in iOS 6, clients can register a nib or class for each cell.
/ / If all reuse identifiers are registered, use the newer-dequeueReusableCellWithIdentifier: forIndexPath: to guarantee that a cell instance is returned.
/ / Instances returned from the new dequeue method will also be properly sized when they are returned.
- (Void) registerNib: (UINib *) nib forCellReuseIdentifier: (NSString *) identifier NS_AVAILABLE_IOS (5_0);
- (Void) registerClass: (Class) cellClass forCellReuseIdentifier: (NSString *) identifier NS_AVAILABLE_IOS (6_0);

Note the comments above
3 For example, you have used NIB made a Cell, or customize a Cell. We UITableView when you create, you can incidentally
 self.tableView.backgroundColor = xxxx; 
[Self.tableView registerClass: [CustomCell class] forCellReuseIdentifier: @ "CustomCell"];


So you are in - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath this method, you can save these codes:
 static NSString * CellIdentifier = @ "Cell"; 
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
/ / Set your cell
}

But only
 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "Cell" forIndexPath: indexPath];


This is enough, and you now understand me?
Reply:
I use xcode 4.5

Seems to come in a uitableviewcell tableview drag and named accordingly CellIdentifier can also solve the corresponding problem

But

A separate tableviewcontroller be useful with the above method,

But when I'm in the sub tableviewcontroller to do the appropriate action when they die, I do not know the code there is something wrong place, or how?

No comments:

Post a Comment