xCode:TableView无法查看/访问所有单元格

时间:2014-08-13 19:19:57

标签: objective-c xcode uitableview ios7

当我按下名为" AllOK"的按钮时我希望object.selectedIndex为0.这与可见的单元格完美配合。但它无法访问在应用程序/屏幕上无法查看的单元格。如果你向下滚动并获得它们的视野,它会检查它们,但我希望它能够做到这一点,而不必向下滚动。

有谁知道如何让tableview知道它"还"得到了看不见的细胞?

我的tableview和按钮的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FancyCell"];
    cell = nil;
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FancyCell"];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        // add the segmentedControl when you create a new cell
        UIImage *correctImageGreen = [[UIImage imageNamed:@"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *correctImageGul = [[UIImage imageNamed:@"gul.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *correctImageRed = [[UIImage imageNamed:@"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *correctImageGray = [[UIImage imageNamed:@"gray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        NSArray *itemArray = [NSArray arrayWithObjects: correctImageGreen, correctImageGul, correctImageRed, correctImageGray, nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
        segmentedControl.frame = CGRectMake(310, 7, 150, 30);
        [cell.contentView addSubview:segmentedControl];

        // add an action so we can change our model if the view changes
        [segmentedControl addTarget:self action:@selector(didChangeSegmentedControl:) forControlEvents:UIControlEventValueChanged];

        // use a tag so we can retrieve the segmentedControl later
        segmentedControl.tag = 42;
    }
    // either if the cell could be dequeued or you created a new cell,
    // segmentedControl will contain a valid instance
    UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell.contentView viewWithTag:42];

    UIImage *comment = [UIImage imageNamed:@"Checkmark-hvid"];
    UIImage *imageRef = [UIImage imageNamed:@"Checkmark-hvid"];
    UIImageView *commentView = [[UIImageView alloc] initWithImage: comment];
    UIImageView *imageRefView = [[UIImageView alloc] initWithImage: imageRef];
    commentView.frame = CGRectMake(625, 5, 30, 30);
    imageRefView.frame = CGRectMake(515, 5, 30, 30);
    commentView.tag = 98;
    imageRefView.tag = 99;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    boolean_t didGetStates = [defaults boolForKey:@"didGetStates"];

    MBFancyObject *object = _objects[indexPath.row];
    if (didGetStates) {

        // State
        NSDictionary *dic = [tableData objectAtIndex:indexPath.row];
        if (object.beingEdited == -1) {
            int selectedState = [[dic valueForKey:@"State"] intValue];
            object.selectedIndex = selectedState;
        }

        // Comment & ImageRef
        int comment = [[dic valueForKey:@"Comment"] intValue];
        int imageRef = [[dic valueForKey:@"Foto"] intValue];
        if (comment == 0) {
            [cell.contentView addSubview:commentView];
        }
        else {
            [[cell.contentView viewWithTag:98]removeFromSuperview];
        }
        if (imageRef == 0) {
            [cell.contentView addSubview:imageRefView];
        }
        else {
            [[cell.contentView viewWithTag:99]removeFromSuperview];
        }

    }
    cell.textLabel.text = object.title;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    segmentedControl.selectedSegmentIndex = object.selectedIndex;
    object.currentIndexRow = indexPath.row;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];

    return cell;
}

- (IBAction)allOK:(id)sender {
    for (MBFancyObject *object in _objects) {
        object.selectedIndex = 0;
        object.beingEdited = 0;
    }
    [[self tableView] reloadData];
}

1 个答案:

答案 0 :(得分:0)

所以我在第一次评论时没有注意到这一点,但你的问题很明显。您没有正确地从单元池中取消单元格,因此在滚动时,您每次都会创建一个全新的单元格,这会导致它们无法按您希望的方式获得所选索引。我很确定你的解决方案就在这里:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FancyCell"];
cell = nil;
if (!cell) {

你正确地将一个单元格出列,然后立即将其设置为nil,这意味着它将始终通过!单元格检查并始终创建一个新单元格。尝试删除此行并使用出列单元格。

编辑:

既然没有这样做,这就是我要尝试的其他方法: 创建对方法willDisplayCell:forRowAtIndexPath:的调用,并在此调用中检查您设置的布尔标志,以查看该索引处的分段控件是否应该在索引0处。所以基本上每当要显示一个单元格时,你要检查它的分段控制是否应该设置为索引0,如果它应该,则将它的索引设置为0.