Tableview没有显示任何数组

时间:2014-08-02 00:45:36

标签: ios objective-c uitableview

我正在尝试创建应用内设置表格视图,该视图需要具有不同内容的不同部分。我首先尝试了外面的所有代码(!cell),但这导致了滚动内容的重复,然后我发现了线程,它说我需要放入内部(!cell),但现在它没有显示任何内容。我做错了什么?

的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier];
    cell.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
    self.emailTextField.delegate = self;


    self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 42, 20)];
    self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
    self.cellName.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
    [cell.contentView addSubview:self.cellName];

    if (indexPath.section == 1) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;


        self.cellName.text = [notArray objectAtIndex:indexPath.row];

        if (indexPath.row == 0) {
            self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.acceptanceSwitch];
        } else if (indexPath.row == 1) {
            self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.messageSwitch];
        } else if (indexPath.row == 2) {
            self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.likeSwitch];
        }
    } else if (indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        self.cellName.text = [accountArray objectAtIndex:indexPath.row];


        if (indexPath.row == 0) {
            self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
            self.nameTextField.enabled = NO;
            [cell.contentView addSubview:self.nameTextField];
        } else if (indexPath.row == 1) {
            self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
            [cell.contentView addSubview:self.emailTextField];
            [self.emailTextField setReturnKeyType:UIReturnKeyDone];
            [self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
        } else if (indexPath.row == 2) {
            self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.genderSwitch];
        }



    }


}



if (indexPath.section == 0) {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    self.cellName.text = [accountArray objectAtIndex:indexPath.row];



} else if (indexPath.section == 1) {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    self.cellName.text = [notArray objectAtIndex:indexPath.row];






} else if (indexPath.section == 2) {


    self.cellName.text = [policyArray objectAtIndex:indexPath.row];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

} else if (indexPath.section == 3) {


    self.cellName.text = [buttonsArray objectAtIndex:indexPath.row];
    self.cellName.textAlignment = NSTextAlignmentCenter;

}



return cell;
}

2 个答案:

答案 0 :(得分:0)

请尝试以下方法:

  1. 使用公共属性(label,textfield)创建UITableViewCell的子类。如果您对不同部分的单元格有不同的要求,那么您可能应该有不同的单元格子类。

  2. 使用视图中的tableview注册这些单元类并加载。例如

    [self.tableView registerClass:[MySectionOneTableViewCell class] forCellReuseIdentifier:MySectionOneIdentifier]; [self.tableView registerClass:[MySectionTwoTableViewCell class] forCellReuseIdentifier:MySectionTwoIdentifier];

  3. 在下面的方法中,只需在检查部分后将单元格出列并设置属性。

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { UITableViewCell 单元格; if(indexPath.section == 0){     cell =(MySectionOneTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MySectionOneIdentifier forIndexPath:indexPath;     cell.textLabel = @" foo&#34 ;;
      } 返回细胞; }
  4. 实现方法,准备在自定义uitableviewcell类中重用,初始化所有标签和文本字段。该方法准备单元的视图以供重用。 e.g。

    • (void)prepareForReuse { self.textLabel.text = @"&#34 ;; }

答案 1 :(得分:-1)

正如@rmaddy所提到的,你不应该在你的(!细胞)中提到细胞的产生。 "代码插图"如下:

- (void)viewDidLoad {
    // This part wasn't included in any specific if/else condition dealing with cell properties (like section or row), not either being modified during the cell generation
    self.emailTextField.delegate = self;
    self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 42, 20)];
    self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
    self.cellName.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
}

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

    // This part ensure you will initiate each cell with the correct identifier
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
        cell.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
    }

    [cell.contentView addSubview:self.cellName];

    // You should check your code and commute the common condition together
    if (indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.cellName.text = [accountArray objectAtIndex:indexPath.row];

        // Be aware that your first section will contain only 3 rows
        if (indexPath.row == 0) {
            self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
            self.nameTextField.enabled = NO;
            [cell.contentView addSubview:self.nameTextField];
        } else if (indexPath.row == 1) {
            self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
            [cell.contentView addSubview:self.emailTextField];
            [self.emailTextField setReturnKeyType:UIReturnKeyDone];
            [self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
        } else if (indexPath.row == 2) {
            self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.genderSwitch];
        }
    } else if (indexPath.section == 1) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.cellName.text = [notArray objectAtIndex:indexPath.row];

        // Be aware that your second section will contain only 3 rows
        if (indexPath.row == 0) {
            self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.acceptanceSwitch];
        } else if (indexPath.row == 1) {
            self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.messageSwitch];
        } else if (indexPath.row == 2) {
            self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
            [cell.contentView addSubview:self.likeSwitch];
        }
    } else if (indexPath.section == 2) {
        // Here you are not adding any specific content to your section 2
        self.cellName.text = [policyArray objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    } else if (indexPath.section == 3) {
        // The same, your fourth section won't content any specific content
        // You should add something like [cell addSubview:MyCustomView]
        self.cellName.text = [buttonsArray objectAtIndex:indexPath.row];
        self.cellName.textAlignment = NSTextAlignmentCenter;

    }

    return cell;
}

正如我在评论中提到的,如果您的本地属性(cellNamenameTextFieldgenderSwitch等)得到很好的实施,那么您只需要6行你的桌子(第一部分为3,第二部分为3。你的第三和第四部分是空的,因为你没有提及其中的任何子视图。

最后,既然你的代码对我来说有很多困惑,我可能会在我的重写中犯了一些错误,所以在这种情况下请随时纠正我。

跳跃它将有助于你的工作。