hachinoBlog

hachinobuのエンジニアライフ

UITableViewCellにaddSubViewしたViewを変更するやり方

xibファイルでカスタムセルを作成した。
セルにはボタンビューを載せて、そのボタンがタッチされる度にボタンの色が変わるようにする。
その時につまづいたのでメモ

- (void)doCellStarButtonTapped:(id)sender event:(id)event
{
 	NSSet *touches = [event allTouches];
	UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:aTableView];
        CGPoint position = [touch locationInView:self.view];

        //タッチ位置からNSIndexPathを取得してセル情報を取得する
	NSIndexPath *indexPath = [aTableView indexPathForRowAtPoint: currentTouchPosition];
        UITableViewCell *cell = [aTableView cellForRowAtIndexPath:index];
       
        //取得したセル情報
        for (UIView *view in cell.subviews) {
            if ([[[view class] description] isEqualToString:@"UITableViewCellContentView"]) {
                for (UIView *btnView in view.subviews) {
                    if ([btnView isMemberOfClass:[UIButton class]]) {
                        [(UIButton *)btnView setSelected:checked];
                    }
                }
            }
        }
}

ここで注目すべきは取得したcellにsubviewsすると返却されたclassがUITableViewCellContentViewだということ。
てっきりcellに対してsubviewsすればお目当てのボタンクラスが返ってくると思っていた。
しかもUITableViewCellContentViewなんてクラスは存在しないのでisMemberOfClassが使えない。
そこで[[[view class] description] isEqualToString:@"UITableViewCellContentView"]と文字列として比較した。
UITableViewCellContentViewに対してsubviewsしたらお目当てのボタンクラスがありました。
それにしてもUITableViewCellContentViewって何だろう..