2012-05-01 64 views
15

我有一個自定義的tableViewCell。我想通過突出顯示來指示用戶觸摸。小區選擇風格是UITableViewCellSelectionStyleBlue。在父控制器中,我設置了self.clearsSelectionOnViewWillAppear = YESUITableViewCell。如何在觸摸時突出顯示單元格?

我應該很好去。不。選擇仍然堅持細胞。我想要的僅僅是觸摸時間的選擇指示。觸摸時應立即返回未選定的外觀。

我如何做到這一點?

乾杯,
道格

回答

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 
+0

我們有一個winna!對於家裏的人來說,實際的方法簽名是deselectRowAtIndexPath:animated :.甜。乾杯。 – dugla

+0

我不敢相信我花了這麼多錢來解決這個痛苦問題... 你只是覺得可可觸摸會做到這一點didHighlightItemAtIndexPath 任何方式謝謝@Dancreek –

0

覆蓋- (void)setSelected:(BOOL)selected animate:(BOOL)animated不調用超

+0

細胞僅選擇觸摸之後 - 問題想要觸摸時改變顏色。 – Zorayr

-3

我有同樣的issue.The下面的代碼完全爲我工作。

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

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

你真的想重新加載每個觸摸事件的表嗎?如何做主題是絕對錯誤的。 – surfrider

+0

這會將單元格變成藍色,但它保持原樣並僅在touchUpInside上起作用。只有觸摸了嗎? –

+0

僅在觸摸後才標記單元格 - 問題想要在觸摸時更改顏色;另外,我同意@surfrider,爲什麼要重新加載數據? – Zorayr

0

這工作:

它得到backgroundview與單元格邊框看起來像seperator.Do在不改變接口builder.Make確保UITableViewCellSelectionStyleNone默認的實現代碼如下設置未設置爲selectionstyle。我正在粘貼工作代碼。 :

- (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

下面夫特示例使用didHighlightRowAtIndexPath改變單元格的背景顏色上向下觸摸並didUnhighlightRowAtIndexPath重置顏色 - 見下文:

// 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

這是行不通的。顏色不會在觸摸時重置 –

+0

您可以在'didUnhighlightRowAtIndexPath:'中放置斷點並確認它是在觸摸時調用的嗎?如果你可以證實這一點,那麼下一個罪魁禍首可能是你的變色邏輯。 – Zorayr

+0

對不起,我意識到我的問題是我在故事板中選擇了「延遲內容」。 –

相關問題