Monday, September 23, 2013

Customized tableviewcell (label + UITextField) will cause the program to crash

Custom table view cell, which contains a UILabel, a UITextField, when the mouse clicks textfield to prepare input, the program will crash, report on the information: [NSObject respondsToSelector]: message sent to deallocated.

code is as follows:
Custom cell:
ComboCell.h

@ Interace ComboCell: UITableViewCell
{
UITextField * textField;
UILabel * name;
}
@ Property (strong, nonatomic) IBOutlet UITextField * textField;
@ Property (strong, nonatomic) IBOutlet UILabel * textField;
@ End

ComboCell.m
...
- (BOOL) textFieldShouldReturn: (UITextField *) textField
{
[TextField resignFirstResponder];
return YES.
}
There is also a comboCell.xib, which is inside the UITableViewCell add le label and textField, and the delegate of the textField set combocell.

Using this cell has the following files in the main stuff:

- (Void) viewDidLoad
{
[Super viewDidLoad];

[Self. tableview registerNib: [UINibnibWithNibName: @ ComboCell "bundle" nil] forCellResuseIdentifier: @ ComboCellId "];
}

. . .


- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
static NSString * cellID = @ "ComboCellId";
ComboCell * cell = (ComboCell *) [tableView dequeueReusableCellWithIdentifier: cellId forIndexPath: indexpath];
return cell;
}

I noticed a phenomenon is this: If I do not put textField's delegate is set to ComboCell, it will not appear crash phenomenon, it seems to me to create a table cell with a nib on.

Want expert guidance is what causes crash, how to fix? I have a lot of different cell need to customize, and I do not want to textField's delegate designated as the tableview controller. Wanted every module are relatively independent.
Multi Xie Daxia help!

Reply:
You do not have to allocate memory cell
Reply:
if (cell == nil)
{
/ / To allocate a memory and initialize CELL
}
Reply:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
static NSString * cellID = @ "ComboCellId";
ComboCell * cell = (ComboCell *) [tableView dequeueReusableCellWithIdentifier: cellId forIndexPath: indexpath];
if (cell == nil)
{
cell = [ComboCell alloc] init ......]; methods themselves, as the case may be,
}
return cell;
}
Reply:
We're not right. In fact, using the nib to construct the object, which has no need to do in ios6 cell = nil's judgment. Carefully read the documentation on apple understand the why. I fix this problem out.
TextField in the nib remove the delegate inside the development,
override function awakeFromNib ()
Specified here textField's delegate.

{
textField.delegate = self;
}

Then on OK. Still thank you for your answer.

No comments:

Post a Comment