[英]UITableView inside UITableViewCell not receiving touch events
[英]Handling Touch Events to Multiple Objects via Custom UITableViewCell and UITableView
我正在處理一個UITableView,它將包含兩種不同類型的自定義UITableViewCells,每個單元格設置為UITable的不同部分。 我希望單元格在觸摸時突出顯示為其他顏色,然后在觸摸結束后恢復為以前的顏色。 由於這沒有遵循標准UITableViewCell的標准觸摸突出顯示實現,因此我向自定義單元格類添加了TouchesBegan()和TouchesEnd()方法以獲得此效果。 但是,UITable不會捕獲任何接觸,因此不會執行我為其他標准表功能所需的didSelectRowAtIndexPath()或deselectRowAtIndexPath()方法。 如果我注釋掉自定義單元格的TouchesBegan / TouchesEnd方法,則UITable將捕獲觸摸並調用這些方法。 有沒有一種方法可以讓自定義單元格執行其觸摸方法,以及讓UITable在一次觸摸時執行didSelect / deselect方法? 這是我第一次在UITable上進行破解,並具有預期的自定義功能,我為此付出了很多努力。 抱歉,代碼中除了我的問題以外的任何地方都不合適,並在此先感謝您的建議。 這是下面這些部分的代碼:
自定義單元格的觸摸方法:
-(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) {
self.currentlySelected = YES;
self.accessoryButton.hidden = NO;
}
else {
self.currentlySelected = NO;
self.accessoryButton.hidden = YES;
}
}
}
-(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];
}
}
對於UITableViewController中的didSelect / DeselectRow方法:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.tableView.bounds.size.width, 30.0)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
CGFloat isRed = 84.0/255.0;
CGFloat isGreen = 84.0/255.0;
CGFloat isBlue = 84.0/255.0;
self.headerColor = [[UIColor alloc]initWithRed:isRed green:isGreen blue:isBlue alpha:0.5];
headerLabel.backgroundColor = self.headerColor;
headerLabel.opaque = NO;
headerLabel.textColor = self.textColor;
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:18.0];
headerLabel.frame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, 30.0);
if (section == 0) {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = @"Alley";
[headerView addSubview:headerLabel];
}
else {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = @"Bowlr";
[headerView addSubview:headerLabel];
}
return headerView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.lastIndexPathForAlleyCells == indexPath) {
[self.tableView beginUpdates];
[self.tableView deselectRowAtIndexPath:self.lastIndexPathForAlleyCells animated:NO];
[self.tableView endUpdates];
}
if(indexPath.section == 0) {
[self.tableView beginUpdates];
[self.tableView deselectRowAtIndexPath:self.lastIndexPathForAlleyCells animated:NO];
AlleyTableViewCell *cell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryButton.hidden = NO;
self.lastIndexPathForAlleyCells = indexPath;
[self.tableView endUpdates];
}
else if(indexPath.section == 1) {
PlayerTableViewCell *cell = (PlayerTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.accessoryButton setImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateNormal];
[cell setSelected:NO animated:NO];
}
}
使用UIGestureRecognizer代替touch delegates
。
要么
在didSelectRowAtIndexPath
上獲取相應的單元格,然后執行所需的任何操作。
如果您希望單元格的選擇顏色不同於默認的藍色,只需在cellForRowAtIndexPath方法中設置單元格的backgroundView屬性即可:
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [UIColor colorWithRed:0.90f green:0.90f blue:0.90f alpha:1.00f];
cell.selectedBackgroundView = backgroundView;
這個問題的答案實際上是對某個普遍問題的一個普遍回答。 考慮到對UITable進行單元格的自定義,關於如何實現這一點可能還不那么明顯。
它與處理自定義單元中的觸摸事件有關,然后將觸摸事件也發送到父視圖(在本例中為UITable)。
這是幫助解決此問題的帖子的鏈接:
當您覆蓋子類中的touch方法時,請寫出您希望看到子類執行的功能,每個方法的末尾包括:
[super touchesBegan:touches withEvent:event];
對每種觸摸方法執行完全相同的操作:touchesMoved,touchesEnd,touchesCancelled。
我的UITable現在接收觸摸事件,並且didSelectRowAtIndexPath和deselectRowAtIndexPath方法現在可以工作,因為它們接收觸摸事件。 我希望這可以幫助其他人!
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan: touches withEvent: event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved: touches withEvent: event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded: touches withEvent: event];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.