当Datasource大于行号时,UITableViewCell / UITextField会产生问题

时间:2013-03-26 08:20:06

标签: iphone ios uitableview uitextfield

所以,这让我疯狂...... 一切都很好,除非我的数据源: NSMutableArrray _names 包含的项目多于 UITableView 中可见的行: namesTable < / strong>即可。 然后一切都崩溃了...细胞彼此重叠,有空细胞,重复细胞。

这是我的设置代码。

    @interface DetailViewController ()
    {
        NSMutableArray *_names;
    }

我的viewDidLoad方法:

    - (void) viewDidLoad
    {
        [super viewDidLoad];
        self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
        [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackOpaque];

        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBack:)];

        self.navigationItem.leftBarButtonItem = backButton;
        if (!_names)
            _names = [[NSMutableArray alloc] init];
    }

和cellForRowAtIndexPath:

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellId = @"MyCustomCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        UITextField *nameTextField;

        if (!cell || refreshCells)
        {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
                                                                  self.view.bounds.origin.y+10,
                                                                  self.view.bounds.size.width-19,
                                                                  26.0)];*/
             nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+10,
                                                                  self.view.bounds.origin.y+4,
                                                                  self.view.bounds.size.width-19,
                                                                  34.0)];
        }

        nameTextField.adjustsFontSizeToFitWidth = YES;
        nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        nameTextField.backgroundColor = [UIColor clearColor];
        nameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        nameTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        nameTextField.textAlignment = UITextAlignmentLeft;
        nameTextField.font = [UIFont fontWithName:@"Noteworthy-Bold" size:22.0];
        nameTextField.keyboardType = UIKeyboardTypeDefault;
        nameTextField.returnKeyType = UIReturnKeyDone;
        nameTextField.clearButtonMode = UITextFieldViewModeNever;
        nameTextField.delegate = self;
        nameTextField.userInteractionEnabled = NO;

        NSString *object = _names[indexPath.row];
        nameTextField.text = [object description];
        [cell.contentView addSubview:nameTextField];
        cell.selectionStyle = UITableViewCellEditingStyleNone;

        return cell;
    }

任何人都可以告诉我,我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以使用XIB设计您的单元格,而不是编写这么多代码。还有什么是refreshCells?您还在哪里向数组添加数据?

最好尝试在XIB和IBOutlet中设计单元格。然后将它加载到cellForRow ...

每次向单元格添加一个文本字段时,在代码中

[cell.contentView addSubview:nameTextField];

根本不需要..

这可以解决您的问题。

答案 1 :(得分:0)

我最终继承了UITableViewCell的子类,并将大部分代码移到了:

@implementation DetailViewCustomCell
@synthesize nameTextField;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        nameTextField = [[UITextField alloc] init];
        nameTextField.adjustsFontSizeToFitWidth = YES;
        nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        nameTextField.backgroundColor = [UIColor clearColor];
        nameTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        nameTextField.textAlignment = UITextAlignmentLeft;
        nameTextField.font = [UIFont fontWithName:@"Noteworthy-Bold" size:22.0];
        nameTextField.returnKeyType = UIReturnKeyDone;
        nameTextField.userInteractionEnabled = NO;

        [self.contentView addSubview:nameTextField];
        self.selectionStyle = UITableViewCellEditingStyleNone;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x+10;
    CGFloat boundsY = contentRect.origin.x+4;
    CGFloat boundsW = contentRect.size.width-19;
    CGRect frame;
    frame= CGRectMake(boundsX ,boundsY, boundsW, 34.0);
    nameTextField.frame = frame;
}

并在我的视图控制器中:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    DetailViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        cell = [[DetailViewCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    NSString *object = _names[indexPath.row];
    cell.nameTextField.text = [object description];
    cell.nameTextField.delegate = self;

    return cell;
}

这完美无缺!