0

表格视图中心单元格总是像选择器一样突出显示。如果我选择中心单元格,则返回该值。如果我在时间中心点滚动表格必须突出显示值仅更改(如选择器操作)如何做到这一点?此任务的任何示例代码。提前致谢。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UILabel     *modelLabel;
        UIButton    *modelButton;
        modelButton                 = [UIButton buttonWithType:UIButtonTypeCustom];
        modelButton.frame           = CGRectMake(0.0, 0.0, LABEL_WIDTH, LABEL_HEIGHT);
        modelButton.tag             = indexPath.row+100;
        [modelButton addTarget:nil action:@selector(modelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [[cell contentView] addSubview:modelButton];
        modelLabel                  = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, LABEL_WIDTH, LABEL_HEIGHT)];
        modelLabel.text             = [NSString stringWithFormat:@"%@", [[modelArray objectAtIndex:indexPath.row] valueForKey:@"Model"]];
        modelLabel.tag              = indexPath.row+1000;
        modelLabel.backgroundColor  = [UIColor clearColor];
        modelLabel.textColor        = [UIColor grayColor];
        modelLabel.alpha            = 0.5;
        modelLabel.textAlignment    = UITextAlignmentCenter;
        modelLabel.font             = EUROSLITE_FONT(14);
        [[cell contentView] addSubview:modelLabel];
    }

    return cell;
}

行数是 700、800,像这样。

4

1 回答 1

1

AUITableView扩展UIScrollView。因此,您可以实现以下委托:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

在那里您可以检查scrollView.contentOffset并将相应的单元格设置为突出显示。请注意,这不是中心单元。您需要添加(int)(scrollView.frame.size.height/2)-(int)(cell.frame.size.height/2).scrollView.contentOffset.y

示例(请取消突出显示所有其他单元格):

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [[tableView visibleCells] makeObjectsPerformSelector:@selector(setHighlighted:) withObject:[NSNumber numberWithBool:NO]];

    CGPoint center = CGPointMake(0, scrollView.contentOffset.y+(int)(scrollView.frame.size.height/2));
    NSIndexPath *cellIndexPath = [tableView indexPathForRowAtPoint:center];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:cellIndexPath];
    [cell setHighlighted:YES];
}
于 2013-01-29T11:02:34.663 回答