需要更改表视图选择的颜色

sma*_*str 24 iphone colors tableview

我需要将表格视图的默认蓝色选择更改为某种自定义颜色.有没有办法做到这一点.帮我

Zor*_*mic 62

最好的方法是这样的:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"myCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIView *v = [[[UIView alloc] init] autorelease];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
    }
    // Set up the cell...
    cell.textLabel.text = @"foo";
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

与您相关的部分是cell.selectedBackgroundView = v;指导.您可以使用您喜欢的任何视图替换此基本视图"v".


小智 6

我还尝试为分组单元格创建自定义选定单元格视图时遇到此问题.我通过为单元格顶部,中间和底部创建3种类型的图像来解决这个问题,假设将存在中间单元格.

NSString *imageFile;

if (row == 0) {
 imageFile = @"highlighted_cell_top.png";
} else if (row == ([registeredDetailsKeys count] - 1)) {
 imageFile = @"highlighted_cell_bottom.png";
} else {
 imageFile = @"highlighted_cell_middle.png";
}

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageFile]];

cell.selectedBackgroundView = imageView;
Run Code Online (Sandbox Code Playgroud)

可能有一种更简单的方法,但我对结果很满意.