2013-04-19 25 views
5

Tôi có thanh công cụ tìm kiếm. Khi tôi chọn ô, tôi muốn toàn bộ ô có [UIColor grayColor];iOS: Màu nền - UITableView và Phụ kiện có cùng màu nền

Với mã bên dưới, màu contentView xuất hiện Màu xám; Tuy nhiên, nền accessoryType màu xuất hiện như màu xanh:

enter image description here

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  

UITableViewCell *cell = [self.searchDisplayController.searchResultsTableView cellForRowAtIndexPath:indexPath]; 
    cell.contentView.backgroundColor = [UIColor grayColor]; 

    if (self.lastSelected && (self.lastSelected.row == indexPath.row)) 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [cell setSelected:NO animated:TRUE]; 
     self.lastSelected = nil; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.accessoryView.backgroundColor = [UIColor grayColor]; // Not working 
     [cell setSelected:TRUE animated:TRUE]; 

     UITableViewCell *old = [self.searchDisplayController.searchResultsTableView cellForRowAtIndexPath:self.lastSelected]; 
     old.accessoryType = UITableViewCellAccessoryNone; 
     [old setSelected:NO animated:TRUE]; 
     self.lastSelected = indexPath; 
    } 

Làm thế nào để làm cho màu xanh cũng xuất hiện như [UIColor grayColor]?

Trả lời

10

Bạn đang thay đổi màu nền của chế độ xem nội dung chỉ là một phần của chế độ xem ô.

UITableViewCell representation

Thay đổi màu nền của toàn bộ tế bào. Tuy nhiên, bạn không thể làm điều đó trong số tableView:didDeselectRowAtIndexPath: vì nó sẽ không hoạt động như được giải thích here.

Note: Nếu bạn muốn thay đổi màu nền của một tế bào (bằng cách thiết lập màu nền của một tế bào thông qua các tài sản kê khai của UIView backgroundColor), bạn phải làm điều đó trong phương pháp tableView:willDisplayCell:forRowAtIndexPath: của các đại biểu và không nằm trong số tableView:cellForRowAtIndexPath: của nguồn dữ liệu.

Trong trường hợp của bạn, hãy theo dõi hàng được chọn trong tableView:didSelectRowAtIndexPath: bằng cách lưu chỉ mục trong thanh ngang và tải lại chế độ xem bảng.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    _savedIndex = indexPath; 
    [tableView reloadData]; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([_savedIndex isEqual:indexPath]) { 
     cell.backgroundColor = [UIColor grayColor]; 
    } 
} 
+0

Cảm ơn bạn đã giải thích chi tiết. Mặc dù tôi tìm thấy một câu trả lời dễ dàng hơn: [cell setSelectionStyle: UITableViewCellSelectionStyleGray]; mà làm việc cho trường hợp của tôi, tôi giả định hầu hết mọi người có thể sẽ đồng ý với câu trả lời của bạn. Nếu bạn cũng có thể bao gồm mã để theo dõi ô đã chọn, tôi muốn đánh dấu câu trả lời của bạn là chính xác. – user1107173

+0

Phải, tôi đã giả sử bạn muốn ô được vẽ bằng một màu tùy chỉnh nhưng khi bạn chỉ ra cho màu xám '[cell setSelectionStyle: UITableViewCellSelectionStyleGray];' hoạt động như sau :-) – andreagiavatto

+0

Mặc dù câu hỏi là để chọn một ô, Tôi đã gặp vấn đề về chế độ xem phụ kiện đó nhiều lần! Để có được xung quanh nó tôi đã phân lớp và vẽ một chữ V tương tự. Lời giải thích này, cộng với sơ đồ tuyệt vời, đã giúp tôi thời gian LỚN. +1 chắc chắn. Cảm ơn bạn @andreagiavatto! – Thawe

Các vấn đề liên quan