この例を使用して、拡張可能なテーブル ビュー セルを実装しようとしています:
http://blog.paxcel.net/blog/expandablecollapsible-table-for-ios/
ここでは、iOS 5 で Xcode 4.3 を使用していますが、 iOS 4.3でXcode4.1を使用して実装する必要があります。このバージョンを使用すると、ボタンがタップされたときにビュー/ボタンが認識されませんが、同じクラスを使用してプロジェクトをコンパイルしましたが、Xcode 4.3/iOS 5 を使用し、タップが認識されるため、クラスは問題ありません。
ボタン付きの UIView を作成するコードは次の
とおりです。
-(id)initWithFrame:(CGRect)frame withTitle:(NSString *)title section:(NSInteger)sectionNumber delegate:(id<SectionView>)Delegate{
self = [super initWithFrame:frame];
if(self){
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(discButtonPressed:)];
[tapGesture setNumberOfTapsRequired:1];
tapGesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:tapGesture];
self.userInteractionEnabled = YES;
self.section = sectionNumber;
self.delegate = Delegate;
CGRect labelFrame = self.bounds;
labelFrame.size.width -= 50;
CGRectInset(labelFrame, 0.0, 5.0);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.text = title;
label.font = [UIFont boldSystemFontOfSize:16.0];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.textAlignment = UITextAlignmentLeft;
[self addSubview:label];
self.sectionTitle = label;
CGRect buttonFrame = CGRectMake(labelFrame.size.width, 0, 50, labelFrame.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
[button addTarget:self action:@selector(discButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.discButton = button;
}
return self;
}
これは、SectionView から作成/委任するクラスです:
TableDropViewController.h
#import <UIKit/UIKit.h>
#import "SectionView.h"
@interface TableDropViewController : UITableViewController <SectionView>
@end
そして、このメソッドはオブジェクト SectionView を作成します:
TableDropViewController.m
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
SectionInfo *si = [self.sectionInfoArray objectAtIndex:section];
if(!si.sectionView){
NSString *title = si.category;
si.sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 45) withTitle:title section:section delegate:self];
}
return si.sectionView;
}
本当に助かります。ありがとう