2つの異なるタイプのカスタムUITableViewCellを含むUITableViewに取り組んでいます。各セルは、UITableの異なるセクションに設定されています。タッチするとセルが別の色にハイライト表示され、タッチが終了すると元の色に戻ります。これは、標準のUITableViewCellの標準のtouch-highlight実装に準拠していないため、この効果を得るために、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];
}
}