我正在尝试使用以下示例实现一个可扩展的表视图单元:
http
://blog.paxcel.net/blog/expandablecollapsible-table-for-ios/
在这里,他们使用 Xcode 4.3 和 iOS 5,但是我必须使用 Xcode4.1 和 iOS 4.3 来实现它。使用这个版本,当点击按钮时,我的视图/按钮无法识别,但我编译了我的项目,使用相同的类,但使用 Xcode 4.3/iOS 5,并且点击它被识别,所以类很好。
下面是使用按钮创建 UIView 的代码:
SectionView.m
-(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;
}
我将非常感谢您的帮助。谢谢