1,
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
2,
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
注釋中可以看出的端倪,第一個是老的接口,第二個新一點,在使用結構上:
第一個方法在
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
要這么寫
cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kTableviewIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:kTableviewIdentifier];
}
第二個方法要這么處理
在定義UITableView位置:
[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableviewIdentifier];
然后在
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
就省掉了判斷為空,可以直接使用
cell = [tableView dequeueReusableCellWithIdentifier:kTableviewIdentifier forIndexPath:indexPath];