2013-07-31 66 views
-3

這是很常見的問題,在SO雖然我有谷歌和交叉檢查我的代碼幾次,但我無法弄清楚失事上按鈕的UITableView CustomCell崩潰點擊

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

我有一個CustomCell命名MyCustomCell與XIB,我有Facebook,Twitter,LinkedIn 3個按鈕。我給他們在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 { 

} 
+0

這些按鈕事件寫在哪裏?你是否真的將所有的行動都綁定在他們身上? –

+0

@Roshni你可以添加你的按鈕動作的代碼。 – icodebuster

+0

按鈕事件寫在MyCustomCell類 – Heena

回答

1

我今天犯了這個錯誤! :)所有你必須做的是取代2行

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

你必須加載loadNibNamed:與.xib文件的名稱。我也認爲它是用標識符,但是這是關於單元格引用的名稱而不是單元格的名稱。

希望這會有所幫助!

+0

這是行不通的,我用CellIdentifier本身與XIB名稱 – Heena

+0

那麼什麼是MyCustomCell? – user2277872

+0

嘗試更改NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@「NameOfCustomCellNibFile」owner:self options:nil]; 到NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@「MyCustomCell」owner:self options:nil]; – user2277872

1
  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的類名更改爲您的類名。 enter image description here
  5. 現在objectAtIndex 0將返回MyCustomCell。
  6. 在你的XIB的這個UITableViewCell中添加你所有的按鈕和其他視圖。
+0

1.兩個名字都一樣。 2.我還沒有與業主嘗試=零 3.On指數0我得到的UITableViewCell,而不是MyCustomCell 4。我還沒有創建IBOutlet中,我只需要IBAction爲 5.Buttons是鑑於自身 – Heena

+0

確定,然後這裏有其他部分的簡單過程: 1.創建一個從UITableViewCell類派生的MyCustomCell類。 2.創建一個與MyCustomCell.xib同名的單獨的.xib文件。 – CodenameLambda1

+0

我想讓XIB中的標識符名稱爲「MyCustomCell」。你的這個答案記得我也要交叉檢查。 – Heena

1

你也可以這樣編寫自定義單元格。在自定義單元的筆尖拖動一個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; 
    } 

不要連接IBAction從Nib。只需連接Custom-cell IBOutlate或將獨特的UIButton標籤與nib連接即可。