質問

This is very common question at SO though I have google and cross check my code few times but I am not able to figure out the crash

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

I have a CustomCell named MyCustomCell with XIB where I have 3 buttons for Facebook,Twitter,LinkedIn. I gave them all IBAction in CustomCell class.

When I click on any of them I get this kind of crash.

I am using ARC.

In my ViewController class I have 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 {

}
役に立ちましたか?

解決

I made this mistake just today! :) all you gotta do is replace the 2 lines

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

You have to load the loadNibNamed: with the .xib file's name. I also thought it was with the identifier, but that is regarding the name that is referenced into the cell instead of the nib for the cell.

Hope this helps!

他のヒント

you can also code for Custom cell like this way. At nib of custom cell drag one UIButton and set it's tag in Xib. then use bellow Method:-

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;
    }

Do not connect IBAction from Nib. just connect Custom-cell IBOutlate or put Unique UIButton tag from nib.

  1. You've to keep the class name of cell and the XIB names same.
  2. Keep the owner nil generally.
  3. Also, if there is only 1 view ( i.e. 1 UITableViewCell in your case ), then this view is available at index 0 instead of index 1.
  4. Inside the XIB file, make sure you link the button to your view itself, i.e. to IBOutlet or IBAction.
  5. The linked buttons must be already added to your view, not floating outside in XIB. Otherwise they will instantiate and come at other indices of the loaded array. If you don't want a certain button for some time, then just keep it hidden.

So the code can be as below:

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

Further make sure you do the following for creating XIB

Ok, then here goes the other portion simple process:

  1. Create a MyCustomCell class derived from UITableViewCell class.
  2. Create a separate .xib file with same name as MyCustomCell.xib.
  3. Open .xib and delete the existing view in it. drag and drop a new
    UITableViewCell from the objects of editor.
  4. See the image to change the class name of the UITableViewCell in XIB to your class name. enter image description here
  5. Now the objectAtIndex 0 will return MyCustomCell.
  6. Add all your buttons and other views in this UITableViewCell of your XIB.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top