1

所以,这让我发疯了......一切都很好,除非我的数据源:一个NSMutableArrray : _names包含的项目多于UITableView中可见的行数:namesTable。然后所有的地狱都崩溃了……细胞相互重叠,有空的细胞,重复的细胞。

这是我的设置代码。

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

谁能告诉我我做错了什么?

4

2 回答 2

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

这很完美!

于 2013-03-28T00:25:39.717 回答
0

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

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

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

[cell.contentView addSubview:nameTextField];

这根本没有必要..

这可以解决你的问题。

于 2013-03-26T08:41:36.233 回答