简体   繁体   English

segue不使用UITableViewCell alloc,但是dequeueReusableCellWithIdentifier

[英]segue not working with UITableViewCell alloc, but dequeueReusableCellWithIdentifier

I'm using storyboard with UITableView in UINavigationController. 我在UINavigationController中使用了带UITableView的storyboard。 In this UITableView, used custom tableViewCell having interior properties. 在这个UITableView中,使用了具有内部属性的自定义tableViewCell。

- (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

    }

Above code work well with push segue. 上面的代码适用于push segue。 But not when I used 但不是我用的时候

     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];   //not work segue

I used this alloc method for preserve cell's data from reusing cell. 我使用这种alloc方法来保留细胞重复使用细胞的数据。

It's just alloc vs deque.. method difference. 它只是alloc vs deque ..方法的区别。 What am I missing? 我错过了什么?

edit) I know that not using the dequeReusableCell method is bad for the performance reason. 编辑)我知道不使用dequeReusableCell方法对性能原因不利。 But, the number of cells would not be many. 但是,细胞数量不会很多。 This is why I don't need the deque method. 这就是我不需要deque方法的原因。

  1. "not working" means "do not perform push segue", not crash. “不工作”意味着“不要执行push segue”,而不是崩溃。

    It shows cell same like when dequeReusable method used except the disclosure indicator icon at the right of cell. 除了单元格右侧的显示指示符图标外,它显示的单元格与使用dequeReusable方法时相同。 The indicator icon come from storyboard setting. 指标图标来自故事板设置。

    And when I touch the cell, the cell highlighted blue but the push segue does not performed. 当我触摸单元格时,单元格突出显示为蓝色,但不执行push segue。

  2. CustomTableViewCell has 4 properties. CustomTableViewCell有4个属性。 That's all different from UITableViewCell. 这与UITableViewCell完全不同。 Users set the properties at DetailViewController(push segue lead to this). 用户在DetailViewController上设置属性(推送segue导致这个)。 The cell doesn't have IBOutlet ref. 该单元格没有IBOutlet参考。 In MasterViewController(having the tableView), cellForRowAtIndexPath method returns CustomTableViewCell above code. 在MasterViewController(具有tableView)中,cellForRowAtIndexPath方法返回上面代码的CustomTableViewCell。

  3. cellForRowAtIndexPath method adds a on/off button on the left of indicator on CustomTableViewCell And set a tag number for the cell. cellForRowAtIndexPath方法在CustomTableViewCell的指示符左侧添加开/关按钮并为单元格设置标记号。

The use of dequeueReusableCellWithIdentifier is what enables you to use your prototype cell. 使用dequeueReusableCellWithIdentifier可以使用原型单元格。 If you use initWithStyle instead of dequeueReusableCellWithIdentifier , then you don't and you therefore lose any segues, disclosure indicators, other UI appearance that you've defined for those cell prototypes, too. 如果你使用initWithStyle而不是dequeueReusableCellWithIdentifier ,那么你就不会丢失任何segues,披露指标,以及你为这些单元原型定义的其他UI外观。

If you're determined to go this route, you'll have to go "old school" (ie do what we all used to do before cell prototypes) and write your own didSelectRowForIndexPath . 如果你决定走这条路,你将不得不去“老学校”(即做我们以前在细胞原型之前做过的事)并编写你自己的didSelectRowForIndexPath But if you already have that segue defined, let's say you called it "SelectRow", then your didSelectRowForIndexPath can perform that: 但是如果你已经定义了segue,假设你称之为“SelectRow”,那么你的didSelectRowForIndexPath可以执行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [self performSegueWithIdentifier:@"SelectRow" sender:cell];
}

If you need to have your disclosure indicator, then your custom cell routine (or the cellForRowAtIndexPath ) will have to set that manually. 如果您需要公开指标,那么您的自定义单元例程(或cellForRowAtIndexPath )必须手动设置。 And if you add it with 如果你添加它

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

Then you need to manually handle it: 然后你需要手动处理它:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [self performSegueWithIdentifier:@"SelectAccessory" sender:cell];
}

Bottom line, you can get this to work, but you're just doing a lot of extra work and losing losing the performance and memory benefits of dequeuing cells. 最重要的是,你可以让它发挥作用,但你只是做了很多额外的工作,并且失去了退出单元格的性能和内存优势。 I'd heartily encourage you to revisit the decision to not use dequeueCellWithIdentifier . 我衷心鼓励您重新考虑不使用dequeueCellWithIdentifier的决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM