尝试到scrollToTop = yes方法失败
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
去掉系统自带的分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
设置tableViewcell的分割线最左端显示
#pragma mark - 设置tableViewcell的分割线最左端显示
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 怎么去掉TableView分割线左边上的空白(必须两个同时实现)
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
设置cell选中不变色
方法一:self.tableView.allowsSelection=NO;
方法二(cellforrow里面添加):cell.selectionStyle = UITableViewCellSelectionStyleNone;
局部刷新
//1: 局部section刷新
NSIndexSet * nd=[[NSIndexSet alloc]initWithIndex:1];//刷新第二个section
[tableview reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
//2: 局部cell刷新
//刷新第一个section的第二行
NSIndexPath *te=[NSIndexPath indexPathForRow:2 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te,nil] withRowAnimation:UITableViewRowAnimationMiddle];
隐藏UITableView的滚动条以及修改滚动条的颜色
//隐藏
self.tableView.showsVerticalScrollIndicator=NO;
//修改颜色
self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
UITabelView设置cell的线的长度
tabelview.separatorInset = UIEdgeInsetsMake(0,18 , 0, 18);
UITableViewCell上的button获取indexpath
TableViewCell *cell = (TableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
点击空白回收键盘
//点击tableView回收键盘
UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(returnKeyBoard)];
tableViewGesture.numberOfTapsRequired = 1;
tableViewGesture.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:tableViewGesture];
设置headView一起滚动
#pragma mark - 设置头部一起滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
添加侧滑删除图画按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//设置第一个section可以删除
if ( indexPath.section == 0 ) {
return YES;
} else
return NO;
}
-(NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *(^imageWithView)(UIView *) = ^(UIView *view) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//3. 从上下文中获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//4. 关闭图形上下文
UIGraphicsEndImageContext();
return image;
};
UIColor *(^getColorWithLabelText)(NSString *, UIColor *, UIColor *) = ^(NSString *text, UIColor *textColor, UIColor *bgColor) {
/*自定义文字
UILabel *actionLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 78, 230)];
actionLb.font = [UIFont boldSystemFontOfSize:14];
actionLb.text = @"删除";
actionLb.textColor = RedColor;
actionLb.textAlignment = NSTextAlignmentCenter;
actionLb.backgroundColor = bgColor;
*/
//自定义图片
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 280)];
content.backgroundColor =BackGround_COLOR;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 90, 40, 40)];
view.backgroundColor =RedColor;
view.layer.cornerRadius =20;
view.layer.masksToBounds =YES;
UIImageView *deleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(12.5, 12.5, 15, 15)];
deleteImage.image = [UIImage imageNamed:@"deletePic"];
[view addSubview:deleteImage];
[content addSubview:view];
return[UIColor colorWithPatternImage:imageWithView(content)];
};
// 添加按钮
UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" "handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了");
}];
rowAction1.backgroundColor = getColorWithLabelText(@"删除", [UIColor clearColor],BackGround_COLOR);
return@[rowAction1];
}
//删除
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableViewdeleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
}