一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

苹果iOS开发之封装tableview教程

时间:2015-12-23 编辑:简简单单 来源:一聚教程网

大家不同的程序的时候可能经常遇到类似的tableview。很简单,但是又很繁琐,这时候封装一个自己常用的tableview也不失为一个良方,充分的节约自己的时间,也增加自己的编程乐趣,何乐而不为呢。可能对于初学者来说,一直都是用着别人写的很强大空能的插件,对插件感到望尘莫及。今天就通过一个小例子,来为大家打开封装的大门。

废话不多说,上代码,下面是.h文件的代码:

//
//  LeftNavigationTableView.h
//  luhui
//
//  Created by X on 15/11/27.
//  Copyright © 2015年 ZMIT. All rights reserved.
//
 
#import
@class LeftNavigationTableView;
@protocol LeftNavigationTableViewDelegate
 
@optional
 
-(void)LeftNavigationTableViewSelected:(LeftNavigationTableView *)leftNavigationTableView resultString:(NSString *)resultString Row:(int)row; //此处是创建自己的tableview点击的代理方法。
 
@end
 
@interface LeftNavigationTableView : UITableView
@property(nonatomic, weak) id mydelegate;  // 自定义代理
 
-(id)setTitle:(NSString *)title AndArray:(NSArray *)array;   //获取数据
@end
.m的代码如下:

//
//  LeftNavigationTableView.m
//  luhui
//
//  Created by X on 15/11/27.
//  Copyright © 2015年 ZMIT. All rights reserved.
//
 
#import "LeftNavigationTableView.h"
//cell identity
#define kTableViewCell @"UITableViewCell"
//title 的高度
#define kTitleHeight 45
//title 字体颜色
#define kTitleTextColor RGB(169,179,186)
//cell 背景色
#define kBGColor RGB(54,67,76)
//cell  字体
#define kFont FONT(12.0f)
//cell  字体颜色
#define kCellTextColor [UIColor whiteColor]
//隔断线的颜色
#define kCellLineColor [UIColor darkGrayColor]
@interface LeftNavigationTableView()
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,copy)NSString *titleString;
//@property (nonatomic,assign)int width;
@end
@implementation LeftNavigationTableView
 
 
-(id)setTitle:(NSString *)title AndArray:(NSArray *)array
{
    _dataArray = array;
    self.tableHeaderView = [self setHeaderViewWithString:title];
    //设置状态栏返回顶部
    [self setScrollsToTop:YES];
    //设置去除tableview的底端横线
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    //去掉tableview的回弹滚动
    self.bounces =NO;
    [self reloadData];
    return self;
    
}
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
//    _width = frame.size.width;
    self.delegate = self;
    self.dataSource = self;
    
    return self;
}
-(UIView *)setHeaderViewWithString:(NSString *)title
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, kTitleHeight)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:headView.frame];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = title;
    titleLabel.backgroundColor = kBGColor;
    titleLabel.font = kFont;
    titleLabel.textColor = kTitleTextColor;
    [headView addSubview:titleLabel];
    return headView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.height-kTitleHeight)/_dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
//    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:kTableViewCell forIndexPath:indexPath];
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTableViewCell];
    CGRect cellFrame = [self rectForRowAtIndexPath:indexPath];
    NSLog(@"%f",cellFrame.size.height);
    //各标题内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellFrame.size.width, cellFrame.size.height)];
    contentLabel.textColor = kCellTextColor;
    contentLabel.backgroundColor = kBGColor;
    contentLabel.textAlignment = NSTextAlignmentCenter;
    contentLabel.text = _dataArray[indexPath.row];
    contentLabel.font = kFont;
    contentLabel.highlightedTextColor = [UIColor blackColor];
    //隔断线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellFrame.size.width, 1)];
    lineView.backgroundColor = kCellLineColor;
    //选中颜色设置
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cellFrame] ;
    cell.selectedBackgroundView.backgroundColor = kYellowColor;
    if (indexPath.row ==0) {
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    }
    [cell addSubview:contentLabel];
    [cell addSubview:lineView];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.mydelegate respondsToSelector:@selector(LeftNavigationTableViewSelected:resultString:Row:)]) {
        [self.mydelegate LeftNavigationTableViewSelected:self resultString:_dataArray[indexPath.row] Row:(int)indexPath.row];
    }
}
@end
好了,效果图如下,每个cell的高度会自适应,调用-(id)setTitle:(NSString *)title AndArray:(NSArray *)array; 这个函数来初始化,并代理自己代理方法就OK了

热门栏目