Tôi gặp sự cố đơn giản với UITableViewCell
. Điều tôi muốn là thay đổi màu văn bản của ô đã chọn. Trong phương pháp cellForRowAtIndexPath
của tôi, tôi thiết lập:UITableXem ô văn bảnMàu màu
cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];
Nếu selectionStyle
là UITableViewCellSelectionStyleNone
, highlightedTextColor
sẽ không thay đổi. Vì vậy, tôi sử dụng hai phương pháp để thiết lập màu chữ:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];
return indexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];
return indexPath;
}
Nó hoạt động, nhưng khi di chuyển tableview, màu sắc thay đổi lại.
điều gì về việc khai báo NSIndexPath * urIndexPath; trong của bạn .h? sau đó trong phương thức didSelecteRowAtIndexPath của bạn, chuyển indexPath của ô đã chọn tới urIndexPath sau đó reloadData để cập nhật giao diện người dùng, trong cellForRowAtIndexPath của bạn, nói điều gì đó giống như, nếu urIndexPath là == indexPath {set color to orange; } else {bất kỳ màu nào theo mặc định} – janusbalatbat
@Akhildas Khi bạn cuộn bảngViewView cellForRowAtIndex: phương thức sẽ được gọi là always.But không phải các phương thức khác mà bạn có.Better bạn có thể gọi phương thức "WillSelectRowAtIndexPath" bên trong phương thức "cellForRowAtIndex:" để làm cho nó công trinh –