2013-03-19 58 views
0

我正在用自定義tableCells構建一個標準的UITableView。我希望定製的單元格在touchBegan和touchEnd上做些事情。我在自定義單元類中實現了這些觸摸方法,它工作得很好。UITableView的didSelectRowAtIndexPath只調用每隔一個時間

這裏是有趣的開始。花了一段時間才弄清楚爲什麼自定義單元格所在的表格沒有接收到觸摸。我希望單元格能夠通過觸摸來完成某些操作,但也可以讓表格接收該觸摸,以便它可以調用didSelectRow/didDeselectRowAtIndexPath。然後,我添加了[super touchesBegan:touches withEvent:event]到自定義單元類的所有觸摸方法,並且這個表也獲得了單元格觸摸以及......排序。當我運行程序時,前2-3個觸摸註冊到單元格和表格。之後,每次單元格都會繼續註冊一次觸摸,但表格每隔一次只會註冊一次觸摸這種奇怪的行爲讓我難堪。我已經研究了視圖層次結構的概念,以查看是否可能是問題,以及諸如hitTest之類的其他解決方案,但沒有什麼突出顯示的解決方案。有沒有人有任何理論,爲什麼會發生?以下是自定義單元格觸摸代碼和didSelectRowAtIndexPath方法。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches]anyObject]; 

    if([touch view] == self.nameLabel) { 
     CGFloat isRed = 151.0/255.0; 
     CGFloat isGreen = 151.0/255.0; 
     CGFloat isBlue = 151.0/255.0; 

      self.nameLabel.backgroundColor = [[UIColor alloc]initWithRed:isRed green:isGreen blue:isBlue alpha:1.0]; 

    if(!self.currentlySelected) { 

     NSLog(@"if(!self.currentlySelected"); 

     self.currentlySelected = YES; 
     [self.accessoryButton setImage:[UIImage imageNamed:@"Checkmark.png"]forState:UIControlStateNormal]; 
     self.accessoryButton.hidden = NO; 

    } 
    else { 
     NSLog(@"if(self.currentlySelected"); 
     self.currentlySelected = NO; 
     self.accessoryButton.hidden = YES; 
     [self.accessoryButton setImage:nil forState:UIControlStateNormal]; 


    } 

} 
NSLog(@"Touch Began"); 
[super touchesBegan:touches withEvent:event]; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesMoved:touches withEvent:event]; 
} 


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches]anyObject]; 

    if([touch view] == self.nameLabel) { 
     self.nameLabel.backgroundColor = [[UIColor alloc]initWithRed:self.isRed green:self.isGreen blue:self.isBlue alpha:1.0]; 
    } 
    NSLog(@"Touch End"); 
    [super touchesEnded: touches withEvent: event]; 
} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesCancelled:touches withEvent:event]; 
} 

didSelectRowAtIndexPath方法:

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

    NSLog(@"Method didselectRow was Called"); 

//If it is in the Alley section 
if(indexPath.section == 0) { 

    //If the selected cell is the same as the last selected cell 
    if([self.lastIndexPathForAlleyCells isEqual:indexPath]) { 
     NSLog(@"Same"); 
     [self.tableView beginUpdates]; 

     AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:self.lastIndexPathForAlleyCells]; 
     previousCell.currentlySelected = NO; 
     self.lastIndexPathForAlleyCells = nil; 
     previousCell.accessoryButton.hidden = YES; 
     [self.tableView endUpdates]; 
    } 

    //Else the selected cell is not the last selected cell 
    else { 
    [self.tableView beginUpdates]; 

     NSLog(@"Not the same"); 
     AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:self.lastIndexPathForAlleyCells]; 
     previousCell.currentlySelected = NO; 
     previousCell.accessoryButton.hidden = YES; 
    AlleyTableViewCell *cell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
     cell.currentlySelected = YES; 
     cell.accessoryButton.hidden = NO; 


    self.lastIndexPathForAlleyCells = indexPath; 
    [self.tableView endUpdates]; 
    } 
} 

else if(indexPath.section == 1) { 
    NSLog(@"Section 1 shouldn't be here"); 
    PlayerTableViewCell *cell = (PlayerTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
    [cell.accessoryButton setImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateNormal]; 
    [cell setSelected:NO animated:NO]; 
} 


} 

回答

2

而不是覆蓋touchesBegan:withEvent:爲需要處理的選擇時,他們對你應該有你的自定義單元格覆蓋setSelected:animated:setHighlighted:animated: /高亮顯示。

UITableView處理單元格的觸摸,因爲UITableViewCell具有複雜的視圖層次結構。

1

與許多問題一樣,問題不在邏輯中,而在語法上。我愚蠢地命名我的UITable「tableView」。這也恰好是在didSelectRowAtIndexPath中使用的參數UITable的名稱。如果你注意到我在上面發佈的代碼中,所有對didSelectRow()的調用都調用self.tableView。相反,他們應該只是調用tableView。儘管我無法完全解釋爲什麼每隔一次調用didSelectRowAtIndexPath方法,但在更改此方法的參數名稱後,所有內容都可以正常工作!

總之,不要將您的屬性命名爲與參數名稱相同的名稱。

相關問題