2014-11-22 74 views
0

我想使左右滑动并单击tableview单元格的行为完全相同。我找不到直接回答这个..在tableviewcontroller中选择一行并向左滑动表现相同

我有一个的tableView和下面的代码,以确保电池高亮发生在点击

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self selectRowAtIndexPath:indexPath]; 
} 

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 


- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell { 
    cell.contentView.backgroundColor = color; 
    cell.backgroundColor = color; 
} 

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Add your Colour. 
    [self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:[tableView  cellForRowAtIndexPath:indexPath]]; //highlight colour 
} 

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Reset Colour. 
    [self setCellColor:[UIColor whiteColor] ForCell:[tableView cellForRowAtIndexPath:indexPath]];  //normal color 
} 

我也有上向左滑动的手势下面的代码一个单元格

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer 
{ 
    //Get location of the swipe 
    CGPoint location = [gestureRecognizer locationInView:self.tblView]; 

    //Get the corresponding index path within the table view 
    NSIndexPath *indexPath = [self.tblView indexPathForRowAtPoint:location]; 

    //Check if index path is valid 
    if(indexPath) 
    { 
     [self.tblView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
     [self.tblView.delegate tableView:self.tblView didSelectRowAtIndexPath:indexPath]; 
    } 
} 

滑动手势正常发生,即它的行为与点击相同,但不会突出显示该单元格。我试图通过设置单元格的颜色来强制左侧滑动手势中的高光,但它仍然不起作用。有什么建议么 ?谢谢

回答

0

我的工作是SelectionStyleNonecellForRowAtIndexPath。如果我没有设置样式,在选择一个被灰色覆盖的单元格后,它不会立即显示选择颜色。

[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

而且在didSelectRowAtIndexPath,设定你的选择的颜色。

cell.backgroundColor = [UIColor brownColor]; 

您可以删除以下行,因为

它不会调用委托方法 (-tableView:willSelectRowAtIndexPath:或 的tableView:didSelectRowAtIndexPath方法:),也不会发出一个 通知。

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone] 
相关问题