tableview里Cell的小对勾颜色改成别的颜色?
_mTableView.tintColor = [UIColor redColor];
怎么在不新建一个Cell的情况下调整separaLine的位置?
一、_myTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);
二、设置分割线风格(对于自定义cell,如果cell左边顶部有空隙需要以下代码调整)
-(void)viewDidLayoutSubviews
{
// 重写UITableView的方法是分割线从最左侧开始
if ([myTableView respondsToSelector:@selector(setSeparatorInset:)]) {
[myTableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([myTableView respondsToSelector:@selector(setLayoutMargins:)]) {
[myTableView setLayoutMargins:UIEdgeInsetsZero];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
//滑动Cell,隐藏键盘
listTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
TableViewCell分割线设置和隐藏多余cell
myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
2.设置分割线的颜色:
[myTableView setSeparatorColor:[UIColor colorWithRed:68.0/255.0green:68.0/255.0 blue:68.0/255.0 alpha:1.0]];
3.隐藏多余cell方法
{
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
}
禁止cell 点击事件
tableView.allowsSelection = NO;
返回的过程中逐渐取消选中状态的,可以提示刚才点进去的是哪一行,默认的也正是这种效果。
- (void) viewWillAppear: (BOOL)inAnimated {
NSIndexPath *selected = [self.table indexpathForSelectedRow];
if(selected) [self.table deselectRowAtIndexpath:selected animated:NO];
}
初始化
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_selectVC = [[SelectViewController alloc]init];
[self tableViewInit];
}
-(UIView*)navRightBtnInit:(NSString*)title
{
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 45)];
[btn setTitle:title forState:UIControlStateNormal];
// [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.textColor = [UIColor blueColor];
btn.backgroundColor = [UIColor redColor];
return btn;
}
-(void)tableViewInit
{
//初始化
/**
* UITableViewStyle
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, \/默认风格,由用户设置参数,
\/区头部会悬浮,
\/区头部分如果不重写view,默认背景色为浅灰色,如果tableView有背景色,会将灰色和背景混合成新颜色
UITableViewStyleGrouped \/系统预设风格,区头部高度都有默认值,
\/区头部不悬浮
主要区别: Grouped 区头部view不悬浮
};
*/
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_tableView];
//遵循代理方法
_tableView.delegate = self;
_tableView.dataSource = self;
//tableViewStyle
//禁止滚动tableView上下滑动,默认是Yes可滑动
// _tableView.scrollEnabled = NO;
//分隔线
/**
* typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone, 无分隔线
UITableViewCellSeparatorStyleSingleLine, 默认值,灰色分隔线
UITableViewCellSeparatorStyleSingleLineEtched 仅支持分组样式表视图
};
*/
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
UIView *headerView = [[UIView alloc]init];
headerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 30);
headerView.backgroundColor = [UIColor redColor];
UIView *footView = [[UIView alloc]init];
footView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 30);
footView.backgroundColor = [UIColor greenColor];
//页眉
_tableView.tableHeaderView = [self navRightBtnInit:@"页 眉"];
//页脚, UITableViewStyleGrouped 风格的话页脚会强制与前面有一小间距
_tableView.tableFooterView = [self navRightBtnInit:@"页 脚"];
;
}
//一种有几个区,默认1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
//每个区有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
/**
* 例子为2个区,第一区一标为0,二区下标为1,以此类推
*/
if (section==0) {
return 2;
}else{
return 10;
}
}
//设置每个区的区头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
// if (section==0) {
// return 50;
// }else{
// return 30;
// }
return 30;
}
//区头文字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section==0) {
return [NSString stringWithFormat:@"%ld区%@",(long)section+1,@"____"];
}else{
return [NSString stringWithFormat:@"%ld区%@",(long)section+1,@"_+_+_"];
}
}
//自定义区头部视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView = [[UIView alloc]init];
sectionView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 30);
sectionView.backgroundColor = [UIColor redColor];
return nil;
}
//默认cell.textLable 缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 10;
}
非自定义Cell
#pragma mark - 初始化TableViewCell,分为重用Cell和非重
/**
* @param tableView 当前tableView实例
* @param indexPath indexPath.section(区) indexPath.row(行)
*
* @return 返回指定cell,自定义或者用系统的格式
*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//声明一个cell的重用标识,静态变量有利于性能优化
static NSString *CellIndetifer = @"Cell";
//初始化cell并注册重用标识,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndetifer];
/**
* 重用与非重用的区分点
* @概念
* cell创建原理:系统会根据当前tableView的可视高度和cell的高度去创建对应数量的cell,然后存放到内存中,
* 重用:当一个cell消失在屏幕外,侧会把消失的cell delete,新出现的cell则从内存中读取相同标识的cell赋值使用
* 非重用:消失的cell依然会delete 新出现的cell不会从之前已经创建的cell中赋值使用,而是重新进alloc一片新的内存空间
* 性能区别:重用避免了大量的alloc创建,尤其在自定义cell中子view比较多的情况下,可以达到只赋值不用创建新控件的性能速度
*
*/
//!cell if语句是作为重用的判断,如果内存还没创建足够第一次显示在屏幕的cell数量,则创建
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIndetifer];
}
//以下是父类Cell的通用属性
/**
* 1.1最右边图案,系统自带
* @property (nonatomic) UITableViewCellAccessoryType accessoryType;
* enum accessoryType{
UITableViewCellAccessoryDisclosureIndicator, ">"
UITableViewCellAccessoryDetailDisclosureButton, 感叹号+">"
UITableViewCellAccessoryCheckmark, "√"
UITableViewCellAccessoryDetailButton 感叹号
}
*
*/
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
/**
* 1.2自定义一个开关覆盖右边图案
* @property (nonatomic, retain) UIView *accessoryView;
*/
UISwitch *sw=[[UISwitch alloc]init];
[sw addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView=sw;
/**
* 2.点击cell颜色
* @property (nonatomic) UITableViewCellSelectionStyle selectionStyle;
* typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone, 无颜色
UITableViewCellSelectionStyleBlue, ios 6 默认蓝色
UITableViewCellSelectionStyleGray, 灰色 (ios 7 默认颜色)
UITableViewCellSelectionStyleDefault (ios7)
};
从ios 7开始已经是默认灰色 UITableViewCellSelectionStyleGray = UITableViewCellSelectionStyleDefault
如果需要改为选中cell的颜色 可以通过设置属性 selectedBackgroundView
*/
// cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *cellBgView = [[UIView alloc]init];
cellBgView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = cellBgView;
if (indexPath.section==0)
{
cell.textLabel.text = @"一区";
}else{
cell.textLabel.text = @"二区";
}
return cell;
}