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:
- (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]?
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
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
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