1

この機能をiPhoneに実装しようとしていますUITableView。常に、最初と最後の可視セルのみが異なる背景色、たとえば赤で、他のセルの色は白のままにします。スクロール中のスムーズな変化。
私が試してみました:

.mファイル内:

NSIndexPath *firstRow;
UITableViewCell* firstCell;

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

    if (cell == nil) 
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    } 

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    //cell.thumbnailImageView.image = image;

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSArray *visible = [tableView indexPathsForVisibleRows];
    firstRow = (NSIndexPath *)[visible objectAtIndex:0];
    firstCell = [tableView cellForRowAtIndexPath:firstRow];
    firstCell.contentView.backgroundColor=[UIColor redColor];
    NSLog(@"main visible cell's row: %i", firstRow.row);
    [tableView endUpdates];
    firstCell.contentView.backgroundColor=[UIColor redColor];   
}

ただし、上にスクロールしても色は更新されません。

4

2 に答える 2

6

必要に応じて、cellForRowAtIndexPath ですべて実行できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *tableViewCellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellID];
    if (!cell) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellID];

    [[cell textLabel] setText:[NSString stringWithFormat:@"some sting %i", [indexPath row]]];


    NSArray *visibleCells = [tableView visibleCells];

    for (int i = 0; i < [visibleCells count]; i++) {
        UITableViewCell *cell = [visibleCells objectAtIndex:i];
        if (i == 0 || i == [visibleCells count] - 1)
            [cell setBackgroundColor:[UIColor redColor]];
        else 
            [cell setBackgroundColor:[UIColor clearColor]];
    }

    return cell;
}
于 2012-07-26T17:45:01.973 に答える
-1

あなたの

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

このようなことを試してください

//Find the first row
if(indexPath.row == 0){
    cell.contentView.backgroundColor = [UIColor redColor];
}

最後の行を確認するには、作成したコードを再利用する必要があります

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
于 2012-07-26T17:23:24.653 に答える