2013-12-13 109 views
1

我有一个UITableViewController,我正在使用的窗体。每个单元格都有一个UILabel和一个UITextField。当我点击UITextField时,键盘出现,然后向下滚动,单元格离开屏幕,当我点击另一个UITextField时,应用程序崩溃。UITableView窗体崩溃

这是我的细胞子类。

@implementation EditorFieldCell 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     self.selectionStyle = UITableViewCellSelectionStyleNone; 

     [self.contentView addSubview:self.nameLabel]; 
     [self.contentView addSubview:self.textField]; 
    } 
    return self; 
} 

- (void)setName:(NSString *)name 
{ 
    _name = name; 

    CGRect frame = self.nameLabel.frame; 
    frame.size.width = roundf([_name sizeWithFont:[UIFont boldSystemFontOfSize:17.0f]].width); 
    self.nameLabel.frame = frame; 

    frame = self.textField.frame; 
    frame.size.width = self.frame.size.width - 16.0f - 14.0f - 14.0f - self.nameLabel.frame.size.width; 
    frame.origin.x = 16.0f + 14.0f + self.nameLabel.frame.size.width; 
    self.textField.frame = frame; 

    self.nameLabel.text = _name; 
} 

- (void)setPlaceholder:(NSString *)placeholder 
{ 
    _placeholder = placeholder; 

    self.textField.placeholder = placeholder; 
} 

- (void)setText:(NSString *)text 
{ 
    _text = text; 

    self.textField.text = text; 
} 

- (UILabel*)nameLabel 
{ 
    if (_nameLabel) 
    { 
     return _nameLabel; 
    } 

    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, 0.0f, 0.0f, self.frame.size.height)]; 
    _nameLabel.font = [UIFont boldSystemFontOfSize:17.0f]; 
    return _nameLabel; 
} 

- (UITextField*)textField 
{ 
    if (_textField) 
    { 
     return _textField; 
    } 

    _textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, self.frame.size.height)]; 
    _textField.font = [UIFont systemFontOfSize:17.0f]; 
    _textField.textAlignment = NSTextAlignmentRight; 
    _textField.keyboardAppearance = UIKeyboardAppearanceDark; 
    return _textField; 
} 

@end 

这里是我的表子类。

@interface ManageWineViewController() 

@end 

@implementation ManageWineViewController 

- (id)init 
{ 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) { 
     self.title = @"Manage Wine"; 
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(done)]; 
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)]; 

     NSMutableArray *data = [[NSMutableArray alloc] init]; 

     NSMutableDictionary *section = [[NSMutableDictionary alloc] init]; 
     NSMutableArray *sectionData = [[NSMutableArray alloc] init]; 

     [sectionData addObject:@{@"name": @"Estate", @"placeholder": @""}]; 
     [sectionData addObject:@{@"name": @"Wine", @"placeholder": @""}]; 
     [sectionData addObject:@{@"name": @"Vintage", @"placeholder": @"", @"keyboardType": [NSNumber numberWithInt:UIKeyboardTypeDecimalPad]}]; 
     [section setObject:sectionData forKey:@"data"]; 

     [data addObject:section]; 

     section = [[NSMutableDictionary alloc] init]; 
     sectionData = [[NSMutableArray alloc] init]; 

     [sectionData addObject:@{@"name": @"Type"}]; 
     [sectionData addObject:@{@"name": @"Style", @"placeholder": @"Select a Style", @"options": @[@"", @"Red", @"White", @"Rosé", @"Sparkling", @"Saké", @"Dessert, Sherry, and Port"]}]; 
     [sectionData addObject:@{@"name": @"Appellation", @"placeholder": @""}]; 
     [section setObject:sectionData forKey:@"data"]; 

     [data addObject:section]; 

     section = [[NSMutableDictionary alloc] init]; 
     sectionData = [[NSMutableArray alloc] init]; 

     [sectionData addObject:@{@"name": @"Alcohol %", @"placeholder": @"", @"keyboardType": [NSNumber numberWithInt:UIKeyboardTypeDecimalPad]}]; 
     [section setObject:sectionData forKey:@"data"]; 

     [data addObject:section]; 

     self.data = data; 

     self.inputTexts = [[NSMutableDictionary alloc] initWithDictionary:@{@"0": @"", 
                      @"1": @"", 
                      @"2": @"", 
                      @"10": @"", 
                      @"11": @"", 
                      @"12": @"", 
                      @"20": @""}]; 
    } 
    return self; 
} 

- (void)done 
{ 
    [self.currentTextField resignFirstResponder]; 

    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 
} 

- (void)save 
{ 
    [self done]; 
} 

- (void)hidePicker 
{ 
    [self.selectActionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.data.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return ((NSArray*)[[self.data objectAtIndex:section] objectForKey:@"data"]).count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *cellInfo = [((NSArray*)[[self.data objectAtIndex:indexPath.section] objectForKey:@"data"]) objectAtIndex:indexPath.row]; 

    static NSString *CellIdentifier = @"EditorCell"; 
    EditorFieldCell *cell = (EditorFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) 
    { 
     cell = [[EditorFieldCell alloc] init]; 
    } 

    cell.textField.tag = [[NSString stringWithFormat:@"%i%i", indexPath.section, indexPath.row] integerValue]; 
    cell.textField.delegate = self; 

    cell.name = cellInfo[@"name"]; 
    cell.placeholder = cellInfo[@"placeholder"]; 
    cell.text = [self.inputTexts objectForKey:[NSString stringWithFormat:@"%i", cell.textField.tag]]; 

    if (cellInfo[@"keyboardType"]) 
    { 
     cell.textField.keyboardType = [cellInfo[@"keyboardType"] integerValue]; 
    } 
    else 
    { 
     cell.textField.keyboardType = UIKeyboardTypeDefault; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
} 

#pragma mark - UITextFieldDelegate methods 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    self.currentTextField = textField; 

    if (textField.tag == 11) 
    { 
     //show select 
     NSArray *options = [[[[self.data objectAtIndex:1] objectForKey:@"data"] objectAtIndex:1] objectForKey:@"options"]; 
     self.selectTextField = textField; 
     self.selectOptions = options; 

     [textField resignFirstResponder]; 

     self.selectActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

     CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 

     self.selectPickerView = [[UIPickerView alloc] initWithFrame:pickerFrame]; 
     self.selectPickerView.showsSelectionIndicator = YES; 
     self.selectPickerView.dataSource = self; 
     self.selectPickerView.delegate = self; 

     [self.selectActionSheet addSubview:self.selectPickerView]; 
     [self.selectActionSheet showInView:[[UIApplication sharedApplication] keyWindow]]; 
     [self.selectActionSheet setBounds:CGRectMake(0, 0, 320, 485)]; 
    } 
} 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField { 
    [self.inputTexts setObject:textField.text forKey:[NSString stringWithFormat:@"%i", textField.tag]]; 
    return YES; 
} 

@end 

,我与碰撞得到的错误是:

*** -[EditorFieldCell _didChangeToFirstResponder:]: message sent to deallocated instance 0x155f6e20 

这是适用于iOS 7(和仅支持iOS的7)是否有帮助。

+0

写这篇文章的一些对象被释放,并且您尝试访问该对象。在配置文件中启用NSZombieEnabled,并添加异常断点以知道发布的确切对象。 –

回答

2

您未使用重用标识符初始化EditorFieldCell

EditorFieldCell *cell = (EditorFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

这种情况将是错误的。

if (!cell) 
    { 
     cell = [[EditorFieldCell alloc] init]; 
    } 

尝试创建细胞与reuseIdentifier

cell = [[EditorFieldCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier]; 

而且在EditorFieldCell.m

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
      self.selectionStyle = UITableViewCellSelectionStyleNone; 

      [self.contentView addSubview:self.nameLabel]; 
      [self.contentView addSubview:self.textField]; 
    } 

} 
+1

这样做!不知道为什么,但。所以我一直在创建一个新的小区,而不是重复使用旧的小区。但为什么会导致这次崩溃? –