文明轉載評論是對自己尊重也是對學者的鼓勵
iOS-通過繼承UIScrollView來自定義UITableView實現對代理的詳解
一.自定義UITableView (CustomerTableView繼承於UIScrollView)
#import <UIKit/UIKit.h>
@class CustomerTableView;
@protocol CustomerTableViewDelegate <NSObject>
//返回cell的個數
-(NSInteger)numberCellForCustomerTableView:(CustomerTableView *)customerView;
//返回cell的高度
-(CGFloat)cellHeightForCustomerTableView:(CustomerTableView *)customerView ;
//相當於cell
-(UIView *)customerTableView:(CustomerTableView *)customerView cellForIndex:(NSInteger)index;
//返回標題
-(NSString *)customerTableView:(CustomerTableView *)customerView titleForIndex:(NSInteger)index;
@end
@interface CustomerTableView : UIScrollView
//代理
@property (nonatomic,assign)id<CustomerTableViewDelegate> customerDelegate;
//設置代理和frame
-(id)initWithDelegate:(id<CustomerTableViewDelegate>)aDelegate andFrame:(CGRect)aFrame;
@end
#import "CustomerTableView.h"
@implementation CustomerTableView
-(id)initWithDelegate:(id<CustomerTableViewDelegate>)aDelegate andFrame:(CGRect)aFrame{
if (self = [super initWithFrame:aFrame]) {
self.frame = aFrame;
//通過代理得到多少個cell
int numberCell = (int)[aDelegate numberCellForCustomerTableView:self];
//通過代理得到cell的高度,用來設置cell的佈局
CGFloat cellHeight = [aDelegate cellHeightForCustomerTableView:self];
CGFloat width = CGRectGetWidth(aFrame);
CGFloat height = CGRectGetHeight(aFrame);
//根據cell的個數來設置contentSize
self.contentSize = CGSizeMake(width, numberCell*cellHeight);
//循環的通過代理來拿到cell title 並根據索引i來設置它們的位置,其他的屬性又屬性設置
for (int i=0; i<numberCell; i++) {
UIView *cell = [aDelegate customerTableView:self cellForIndex:i];
cell.frame = CGRectMake(0, cellHeight*i, width, cellHeight);
[self addSubview:cell];
NSString *title = [aDelegate customerTableView:self titleForIndex:i];
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, cellHeight)];
lable.backgroundColor = [UIColor brownColor];
lable.text = title;
[cell addSubview:lable];
}
}
return self;
}
@end
使用:
#import "ViewController.h"
#import "CustomerTableView.h"
@interface ViewController ()<CustomerTableViewDelegate>
@end
@implementation ViewController{
}
- (void)viewDidLoad {
[super viewDidLoad];
CustomerTableView *customer = [[CustomerTableView alloc]initWithDelegate:self andFrame:CGRectMake(0, 0, 320, 400)];
customer.showsHorizontalScrollIndicator = NO;
customer.backgroundColor = [UIColor purpleColor];
[self.view addSubview:customer];
}
//返回cell的高度
-(CGFloat)cellHeightForCustomerTableView:(CustomerTableView *)customerView{
return 100;
}
//返回對應索引的的名字
-(NSString *)customerTableView:(CustomerTableView *)customerView titleForIndex:(NSInteger)index{
return [NSString stringWithFormat:@"item %ld",index];
}
//相當於UITableView的返回cell
-(UIView *)customerTableView:(CustomerTableView *)customerView cellForIndex:(NSInteger)index{
UIView *v = [[UIView alloc]init];
v.backgroundColor = [UIColor redColor];
return v;
}
//返回cell的個數
-(NSInteger)numberCellForCustomerTableView:(CustomerTableView *)customerView{
return 10;
}
@end
二.UITableView的使用
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
代理使用:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//相當於CustomerTableview返回多少個cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
//相當於CustomerTableView 的UIView的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
//相當於CustomerTableview 的UIView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = [NSString stringWithFormat:@"item%ld",indexPath.row];
return cell;
}
運行效果: