文章目錄
大家不同的程序的時候可能經常遇到類似的tableview。很簡單,但是又很繁瑣,這時候封裝一個自己常用的tableview也不失爲一個良方,充分的節約自己的時間。
AppSettingTableView.h
//
// AppSettingTableView.h
// PackageTableView
//
// Created by on 2020/3/30.
// Copyright © 2020 Shae. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class AppSettingTableView;
@protocol AppSettingTableViewDelegate<NSObject>
@optional
-(void)AppSettingTableViewSelected:(AppSettingTableView*)appSettingTableView resultString:(NSString*)resultString Row:(int)row;
@end
@interface AppSettingTableView : UITableView
@property (nonatomic,weak) id <AppSettingTableViewDelegate> mydelegate;
-(id)setTitle:(NSString *)title AndArray:(NSArray *)array;
@end
NS_ASSUME_NONNULL_END
AppSettingTableView.m
//
// AppSettingTableView.m
// PackageTableView
//
// Created by on 2020/3/30.
// Copyright © 2020 Shae. All rights reserved.
//
#import "AppSettingTableView.h"
#import "APPSettingTableViewCell.h"
//cell identity
#define kTableViewCell @"AppSettingTableViewCell"
//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 blueColor]
//隔斷線的顏色
#define kCellLineColor [UIColor darkGrayColor]
@interface AppSettingTableView()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,copy)NSString *titleString;
//@property (nonatomic,assign)int width;
@end
@implementation AppSettingTableView
-(id)setTitle:(NSString *)title AndArray:(NSArray *)array
{
_dataArray = array;
NSLog(@"_dataArray=%@",_dataArray);
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.frame.size.width, kTitleHeight)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:headView.frame];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = title;
titleLabel.backgroundColor = [UIColor whiteColor];;
titleLabel.font = [UIFont
boldSystemFontOfSize:20];
titleLabel.textColor = [UIColor blackColor];
[headView addSubview:titleLabel];
return headView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}
/*-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%f",(self.frame.size.height-kTitleHeight)/_dataArray.count);
return (self.frame.size.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];
APPSettingTableViewCell *cell=[APPSettingTableViewCell cell];
// CGRect cellFrame = [self rectForRowAtIndexPath:indexPath];
// NSLog(@"%f",cellFrame.size.height);
cell.label.text=_dataArray[indexPath.row];
/*//各標題內容
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cellFrame.size.width, cellFrame.size.height)];
contentLabel.textColor = kCellTextColor;
contentLabel.backgroundColor = [UIColor redColor];
contentLabel.textAlignment = NSTextAlignmentCenter;
contentLabel.text = _dataArray[indexPath.row];
NSLog(@"_dataArray[%ld]=%@",(long)indexPath.row,_dataArray[indexPath.row]);
contentLabel.font =[UIFont
boldSystemFontOfSize:20];
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 = [UIColor yellowColor];
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(AppSettingTableViewSelected:resultString:Row:)]) {
[self.mydelegate AppSettingTableViewSelected:self resultString:_dataArray[indexPath.row] Row:(int)indexPath.row];
}
}
@end
APPSettingTableViewCell
使用
//
#import "AppSettingTableView.h"
@interface AppSettingVC ()
@end
@implementation AppSettingVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];
AppSettingTableView *appSettingTableView=[[AppSettingTableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-300)];
[appSettingTableView setTitle:@"TEST" AndArray:array];
appSettingTableView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:appSettingTableView];
}
@end
demo
去掉多餘的分割線
plain類型的tableview當顯示的數據很少時,下面的cell即使不顯示數據也會有分割線,可以通過下面這個函數去掉多餘的分割線。
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
UIView *view =[ [UIView alloc]init];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
[view release];
}
當tableview的dataSource爲空時,也就是沒有數據可顯示時,該方法無效,只能在numberOfRowsInsection函數,通過判斷dataSouce的數據個數,如果爲零可以將tableview的separatorStyle設置爲UITableViewCellSeparatorStyleNone去掉分割線,然後在大於零時將其設置爲
UITableViewCellSeparatorStyleSingleLine(驗證發現不用單獨設置)
如何獲取點擊的是哪個tableViewCell上的Button按鈕
1.首先把cell上button按鈕的點擊方法寫入在cell展示裏面
//cell展示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PraiseListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PraiseListCell" forIndexPath:indexPath];
//重點
[cell.attentionClickButton addTarget:self action:@selector(onTouchBtnInCell:) forControlEvents:(UIControlEventTouchUpInside)];
return cell;
}
2.在button按鈕的點擊方法裏面實現
- (void)onTouchBtnInCell:(UIButton *)sender {
CGPoint point = sender.center;
point = [self.tableView convertPoint:point fromView:sender.superview];
NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];
NSLog(@"%ld",(long)indexpath.row);
}
3.能拿到點擊的indexpath,我們就可以操作數據、數組、做自己要的東西了