2012-03-16 30 views
13

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 selectionStyleUITableViewCellSelectionStyleNone, 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.

+0

đ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

+0

@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 –

Trả lời

19

Got giải pháp bằng cách thiết lập màu sắc trong if (tế bào == nil) kiểm tra

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
}  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor]; 

} 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor]; 

} 

Cảm ơn

14

Tất cả những gì bạn phải làm là

cell.textLabel.textColor = [UIColor whiteColor]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.highlightedTextColor = [UIColor orangeColor]; 

bên trong if (tế bào == nil)

mà sẽ hoạt động tốt.

0

thiết lập một toàn cầu: int m_selectedCell; sửa đổi nó trong

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

chỉ cần thêm

if(m_selectedCell == indexPath.row){ 
... 
... 
}else{ 
} 

trong

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

đó là tất cả!

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