2012-05-01 24 views
15

Tôi có một tableViewCell tùy chỉnh. Tôi muốn cho biết người dùng chạm xuống bằng cách đánh dấu. Kiểu chọn ô là UITableViewCellSelectionStyleBlue. Trong bộ điều khiển chính tôi đã đặt self.clearsSelectionOnViewWillAppear = YES.UITableViewCell. Làm cách nào để tôi chỉ đánh dấu ô trong khi chạm?

Tôi nên làm tốt. Không. Lựa chọn vẫn dính vào tế bào. Những gì tôi muốn là dấu hiệu lựa chọn chỉ trong thời gian chạm xuống. Sự xuất hiện sẽ ngay lập tức trở lại hình thức không được chọn khi chạm vào.

Làm cách nào để thực hiện việc này?

Chúc mừng,
Doug

Trả lời

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
+0

Chúng tôi có một winna! Đối với những người ở nhà, chữ ký phương thức thực tế là deselectRowAtIndexPath: animated :. Ngọt. Chúc mừng. – dugla

+0

Tôi không thể tin rằng tôi dành quá nhiều để tìm ra vấn đề đau này ... Bạn chỉ cần nghĩ rằng chạm vào ca cao sẽ làm điều đó tại didHighlightItemAtIndexPath bất kỳ cách nào nhờ @Dancreek –

0

ghi đè - (void)setSelected:(BOOL)selected animate:(BOOL)animated mà không gọi siêu

+0

Các tế bào được chọn chỉ sau khi chạm vào - câu hỏi muốn thay đổi màu khi chạm vào. – Zorayr

-3

tôi đã cùng mã issue.The dưới đây hoàn toàn làm việc cho tôi.

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

[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor blueColor]]; 
[table reloadData]; 
} 
+2

Bạn có thực sự muốn tải lại bảng trên mọi sự kiện cảm ứng không? Nó hoàn toàn sai cách làm chủ đề. – surfrider

+0

Điều này biến ô màu xanh, nhưng nó vẫn như vậy và nó chỉ hoạt động trên touchUpInside. Điều gì về chạm xuống chỉ? –

+0

Ô được đánh dấu chỉ được chọn sau khi chạm vào - câu hỏi muốn thay đổi màu khi chạm xuống; Ngoài ra, tôi đồng ý với @surfrider, tại sao bạn tải lại dữ liệu? – Zorayr

0

này hoạt động:

Nó được backgroundview với biên giới di động trông như seperator.Do không thay đổi các thiết lập mặc định tableview trong giao diện builder.Make chắc chắn UITableViewCellSelectionStyleNone không được đặt để selectionstyle. Tôi đang dán mã làm việc. :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *kCellIdentifier = @"PlayListCell"; 
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier]; 
} 
MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row]; 
cell.textLabel.text = playList.name; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
// cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]]; 
MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ]; 

cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]]; 

// the main code which make it highlight 

UIView *bgColorView = [[UIView alloc] init]; 
bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f]; 
[bgColorView.layer setBorderColor:[UIColor blackColor].CGColor]; 
[bgColorView.layer setBorderWidth:1.0f]; 
[cell setSelectedBackgroundView:bgColorView]; 


return cell; 

}

9

Dưới dụ Swift sử dụng didHighlightRowAtIndexPath để thay đổi màu nền của tế bào trên cảm ứng xuống và didUnhighlightRowAtIndexPath để thiết lập lại màu sắc - xem dưới đây:

// MARK: UITableViewDelegate 

func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
    cell.backgroundColor = UIColor.greenColor() 
    } 
} 

func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
    cell.backgroundColor = UIColor.blackColor() 
    } 
} 
+0

điều này không hoạt động. Màu không được đặt lại khi chạm vào –

+0

Bạn có thể đặt điểm ngắt trong 'didUnhighlightRowAtIndexPath:' và xác nhận rằng nó được gọi khi chạm vào không? Nếu bạn có thể xác nhận điều đó, thì thủ phạm tiếp theo có thể là logic thay đổi màu sắc của bạn. – Zorayr

+0

xin lỗi tôi đã nhận ra vấn đề của mình là tôi đã 'trì hoãn nội dung chạm' được chọn trong bảng phân cảnh của mình. –

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