0

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];
}
}
4

3 に答える 3

0

この質問に対する答えは、実際には、やや一般的な問題に対するやや一般的な答えです。UITableへのセルのカスタマイズを考慮すると、これにどのようにアプローチするかについてはそれほど明白ではなかった可能性があります。

これは、カスタマイズされたセルでタッチイベントを処理してから、親ビュー(この場合はUITable)にもタッチイベントを送信することと関係があります。

この問題の解決に役立った投稿へのリンクは次のとおりです。

親ビュータッチの処理

子クラスのtouchメソッドを上書きするときは、子に実行させたい機能を書き出し、各メソッドの最後に次のようにします。

[super touchesBegan:touchs 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];
}
于 2013-03-14T17:11:28.213 に答える
0

の代わりにUIGestureRecognizerを使用しtouch delegatesます。

また

対応するセルをオンdidSelectRowAtIndexPathにして、必要なことを行います。

于 2013-03-13T21:31:31.553 に答える
0

セルの選択色をデフォルトの青色とは異なるものにしたい場合は、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;
于 2013-03-13T21:33:50.817 に答える