显示ModalView后,在`cellForRowAtIndexPath`中分配UIPickerView为空

时间:2014-04-04 11:53:50

标签: ios uitableview uipickerview lifecycle

我在UIPickerView分配cellForRowAtIndexPath。这是因为我为它分配了一个标签。它在视图首次加载时有效,但是,当我呈现模态视图控制器,然后将其关闭并返回到包含UIPickerView的表的视图时,选择器为空(没有值)。我在[tableView reloadData中尝试ViewWillAppear,认为它会将选择器与CellForRowAtIndexPath中的值重新分配,但它不会。我使用以下代码分配了picker对象:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = nil;

    [cell = createPickerCell];

    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];

    return cell;

}

-(UITableViewCell *) createPickerCell{
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:kPickerCell];
    self.picker.delegate=self;
    self.picker.dataSource =self;
    self.picker = (UIPickerView *)[cell viewWithTag:223];
    NSLog(@"picker tag is %ld",(long)self.picker.tag);
    [cell addSubview:self.stylistPicker];

    return cell;
}

如果有任何不清楚的地方,请询问。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,非常简单。不是通过单元格中的标签分配它,而是在单元格中创建选择器视图要容易得多。

-(UITableViewCell *) createPickerCell{
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:kPickerCell];
    self.picker = [UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 163)];// new line
    self.picker.delegate=self;
    self.picker.dataSource =self;
    [cell addSubview:self.stylistPicker];

    return cell;

}