【问题标题】:UITableView CustomCell crash on Button Click按钮单击时 UITableView CustomCell 崩溃
【发布时间】:2013-07-31 04:33:19
【问题描述】:

这是一个非常常见的问题,虽然我有谷歌并交叉检查我的代码几次,但我无法找出崩溃

*** -[MyCustomCell performSelector:withObject:withObject:]: message sent to deallocated instance 0x96f6980

我有一个名为 MyCustomCell 的 CustomCell 和 XIB,其中我有 3 个用于 Facebook、Twitter、LinkedIn 的按钮。我在 CustomCell 类中给了他们所有的 IBAction。

当我点击其中任何一个时,我都会遇到这种崩溃。

我正在使用 ARC。

在我的 ViewController 类中,我有 cellForRowAtIndexPath

{
        static NSString *CellIdentifier = @"MyCustomCell";
        MyCustomCell *cell = (MyCustomCell*)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
            cell = (MyCustomCell *)[nib objectAtIndex:1];
        }
        return cell;
}


MyCustomCell.m

- (IBAction)facebookPressed:(BaseButton *)sender {

}
- (IBAction)twitterPressed:(BaseButton *)sender {

}
- (IBAction)linkedInPressed:(BaseButton *)sender {

}

【问题讨论】:

  • 那些按钮事件写在哪里?你真的把所有的动作都绑定到他们身上了吗?
  • @Roshni 你能添加按钮操作的代码吗?
  • 按钮事件写在 MyCustomCell 类中
  • @Roshni 也粘贴该代码..你能加载自定义单元格吗?
  • 是的单元格正在加载。它只是与给定消息崩溃的点击事件

标签: iphone objective-c uitableview custom-cell


【解决方案1】:

我今天犯了这个错误! :) 你所要做的就是替换 2 行

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NameOfCustomCellNibFile" owner:self options:nil];
cell = [nib objectAtIndex:1]; 

您必须使用 .xib 文件的名称加载 loadNibNamed:。我还认为它与标识符有关,但这是关于引用到单元格中的名称而不是单元格的 nib。

希望这会有所帮助!

【讨论】:

  • 这不起作用,我已将 CellIdentifier 本身与 XIB 名称一起使用
  • 那么 MyCustomCell 是什么?
  • 尝试更改 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NameOfCustomCellNibFile" owner:self options:nil]; to NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
  • 是的,我尝试过相同的代码NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil]
【解决方案2】:

您也可以像这样为自定义单元格编码。在自定义单元格的笔尖处,拖动一个 UIButton 并在 Xib 中设置它的标签。然后使用下面的方法:-

UIButton *btnMulSelected;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier =[NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            [[NSBundle mainBundle]loadNibNamed:@"cell_custome_iphones" owner:self options:nil];
            cell = self.tblCell;
            self.tblCell = nil;

            btnMulSelected =(UIButton*)[cell.contentView viewWithTag:2];

            [btnMulSelected addTarget:self action:@selector(MyButtonAction:) forControlEvents:UIControlEventTouchUpInside];


        }
        return cell;
    }

不要从 Nib 连接 IBAction。只需连接 Custom-cell IBOutlate 或从笔尖放置 Unique UIButton 标签。

【讨论】:

    【解决方案3】:
    1. 您必须保持单元的类名和 XIB 名称相同。
    2. 一般保持所有者为零。
    3. 此外,如果只有 1 个视图(即在您的情况下为 1 个 UITableViewCell),则此视图在索引 0 而不是索引 1 处可用。
    4. 在 XIB 文件中,确保将按钮链接到视图本身,即链接到 IBOutlet 或 IBAction。
    5. 链接的按钮必须已经添加到您的视图中,而不是浮动在 XIB 之外。否则,它们将实例化并到达加载数组的其他索引。如果您在一段时间内不想要某个按钮,那么只需将其隐藏即可。

    所以代码可以如下:

    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner: nil options:nil];
    MyCustomCell *cell = (MyCustomCell*)[views objectAtIndex:0];
    

    进一步确保您为创建 XIB 执行以下操作

    好的,接下来是其他部分的简单流程:

    1. 创建一个派生自 UITableViewCell 类的 MyCustomCell 类。
    2. 创建一个与 MyCustomCell.xib 同名的单独 .xib 文件。
    3. 打开 .xib 并删除其中的现有视图。拖放一个新的
      UITableViewCell 来自编辑器的对象。
    4. 查看图片将XIB中的UITableViewCell的类名更改为你的类名。
    5. 现在 objectAtIndex 0 将返回 MyCustomCell。
    6. 在 XIB 的 UITableViewCell 中添加所有按钮和其他视图。

    【讨论】:

    • 1.两个名称相同。 2.我还没有尝试使用 owner=nil 3.在索引 0 上我得到 UITableviewCell 而不是 MyCustomCell 4.我没有创建 IBOutlet 因为我只需要 IBAction 5.按钮在视图中
    • 好的,接下来是另一部分的简单过程: 1. 创建一个派生自 UITableViewCell 类的 MyCustomCell 类。 2. 创建一个与 MyCustomCell.xib 同名的单独 .xib 文件。
    • 我在 XIB 中将标识符名称指定为“MyCustomCell”。你的这个答案记得我也要交叉检查。
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2017-06-15
    • 1970-01-01
    • 2013-09-23
    • 2011-10-20
    • 2015-09-18
    相关资源
    最近更新 更多