Ios segue不使用UITableViewCell alloc,但将ReusableCellWithIdentifier退出队列
我正在UINavigationController中使用带UITableView的情节提要。 在此UITableView中,使用了具有内部属性的自定义tableViewCellIos segue不使用UITableViewCell alloc,但将ReusableCellWithIdentifier退出队列,ios,uitableview,segue,Ios,Uitableview,Segue,我正在UINavigationController中使用带UITableView的情节提要。 在此UITableView中,使用了具有内部属性的自定义tableViewCell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomTableViewCell *cell = nil; if (SYSTEM_VE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = nil;
if (SYSTEM_VERSION_LESS_THAN(@"6.0") ) {
//iOS 6.0 below
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
}
else {
//iOS 6.0 above
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; //work segue
}
以上代码与push segue配合良好。但当我使用
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; //not work segue
我使用这个alloc方法来保存单元格的数据,避免重用单元格
这只是alloc vs deque。。方法差异。我错过了什么
编辑)我知道不使用dequeReusableCell方法对于性能是有害的。但是,细胞的数量不会很多。这就是为什么我不需要deque方法
使用
dequeueReusableCellWithIdentifier
可以使用原型单元。如果您使用initWithStyle
而不是dequeueReusableCellWithIdentifier
,那么您不会这样做,因此您也会丢失您为这些单元原型定义的任何序列、公开指示符和其他UI外观
如果你决定走这条路,你必须走“老派”(也就是说,做我们在细胞原型之前都做过的事情)并编写你自己的didSelectRowForIndexPath
。但是,如果已经定义了该segue,假设您将其称为“SelectRow”,那么您的didSelectRowForIndexPath
可以执行以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"SelectRow" sender:cell];
}
如果您需要有披露指示器,则您的自定义单元格例程(或cellForRowAtIndexPath
)必须手动设置该指示器。如果你加上
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
然后您需要手动处理它:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"SelectAccessory" sender:cell];
}
总之,你可以让它工作,但你只是做了很多额外的工作,失去了退出队列单元的性能和内存优势。我衷心鼓励您重新考虑不使用
dequeueCellWithIdentifier
注意,在使用故事板时,dequeueReusableCellWithIdentifier
将始终为您提供一个单元格。谢谢。清楚明了的回答。谢谢你的建议。