ios UITableView和自定义UITableViewCell

1、自定义cell,FindCell.h

#ifndef FindCell_h
#define FindCell_h

#import <UIKit/UIKit.h>

@interface FindCell : UITableViewCell

@property (strong,nonatomic) UIImageView *leftImage;

@property (strong,nonatomic) UILabel *titleLable;

@property (strong,nonatomic) UILabel *dateLabel;

@end

#endif /* FindCell_h */

FindCell.m

#import "FindCell.h"

@interface FindCell ()

@end


@implementation FindCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initLayout];
    }
    return self;
}

- (void)initLayout {
    self.leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(16, 10, 100, 100)];
    self.leftImage.backgroundColor = [UIColor orangeColor];
    self.leftImage.layer.masksToBounds = YES;
    self.leftImage.layer.cornerRadius = 50;
    // cell提供了一个contentView的属性,专门用来自定义cell,防止在cell布局的时候发生布局混乱,如果是自定义cell,记得将子控件添加到ContentView上
    [self.contentView addSubview:self.leftImage];
    
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.leftImage.frame) + 10, CGRectGetMinY(self.leftImage.frame), 100, 40)];
    self.titleLable.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.titleLable];
    
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.titleLable.frame), CGRectGetMaxY(self.titleLable.frame) + 20, 200, 40)];
    self.dateLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:self.dateLabel];
}

@end


2、调用

#ifndef FindViewController_h
#define FindViewController_h

#import <UIKit/UIKit.h>

@interface FindViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@end

#endif /* FindViewController_h */


#import "FindViewController.h"
#import "FindCell.h"


@interface FindViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *allContactsArray;

@property (weak, nonatomic) IBOutlet UISearchBar *search;

@end


@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.search.delegate = self;
    self.tableView.rowHeight = 70;  默认行高 YES;
    
     将SearchBar绑定到TableView表头
    self.tableView.tableHeaderView = self.search;
    
    self.allContactsArray = [[NSMutableArray alloc]init];
    
    [self handleData];
}

- (void)handleData {
    FindCell *findCell = nil;
    
    for (int i = 0; i < 20; i ++) {
        findCell = [[FindCell alloc] init];
        [findCell.leftImage setImage:[UIImage imageNamed:@"我的.pdf"]];
        findCell.titleLable.text = @"标题";
        findCell.locationLabel.text = @"一区";
        findCell.dateLabel.text = @"20180601";
        [self.allContactsArray addObject:findCell];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

 数量少向上滑动控制在首行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= 0.01) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >= 0.01){
        scrollView.contentInset = UIEdgeInsetsMake(-0.01, 0, 0, 0);
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.allContactsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

// 在有cell数据时候生效
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    FindCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[FindCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    FindCell *findCell = self.allContactsArray[indexPath.section];
     cell点击时颜色
    findCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return findCell;
}

// cell点击时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     离开时消除颜色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

 侧滑删除
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //[tableView setEditing:YES animated:YES];
        [self.allContactsArray removeObjectAtIndex:indexPath.section];
        completionHandler (YES);
        [self.tableView reloadData];
    }];
//    deleteRowAction.image = [UIImage imageNamed:@"删除"];
//    deleteRowAction.backgroundColor = [UIColor redColor];
    
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    config.performsFirstActionWithFullSwipe = NO;  禁止超长左滑直接删除
    return config;
}

 点击搜索后收起键盘
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder]; //searchBar失去焦点
}

@end

关于侧滑删除,会出现崩溃的显现,下面的写法,没有动画效果,但是不会出现崩溃的情况:

 侧滑删除
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self.allContactsArray removeObjectAtIndex:indexPath.section];
        [self.tableView reloadData];
    }];
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    config.performsFirstActionWithFullSwipe = NO;  禁止超长左滑直接删除
    return config;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值