ViewController.m
#import "ViewController.h"
#import "CustomTableViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
//声明tableview
@property (nonatomic, strong) UITableView *tableview;
//数据源数组
@property (nonatomic, strong) NSMutableArray *dataSource;
//刷新控件
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@end
@implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.title = @"table view";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//初始化数据源
_dataSource = [UIFont familyNames].mutableCopy;
//实例化tabelview
_tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//行高
_tableview.rowHeight = 70.0;
//代理
_tableview.dataSource = self;
_tableview.delegate = self;
//注册cell
[_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//添加到视图
[self.view addSubview:_tableview];
//next
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(nextAction:)];
//edit
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:@selector(editAction:)];
//refresh controller
_refreshControl = [[UIRefreshControl alloc]init];
_refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
[_refreshControl addTarget:self action:@selector(beginRefresh:) forControlEvents:UIControlEventValueChanged];
[_tableview addSubview:_refreshControl];
}
- (void)beginRefresh:(UIRefreshControl *)sender{
//延时2秒钟
[self performSelector:@selector(addNewFont) withObject:nil afterDelay:2];
}
//插入新字体
- (void)addNewFont{
//停止刷新
[_refreshControl endRefreshing];
//警告框
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加新字体" preferredStyle:UIAlertControllerStyleAlert];
//添加文本框
[alertController addTextFieldWithConfigurationHandler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//获取文本框
UITextField *textField = alertController.textFields[0];
NSString *newFont = textField.text;
//1.插入数据到数据源
[_dataSource insertObject:newFont atIndex:0];
//2.插入表格行视图(带动画)
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)nextAction:(UIBarButtonItem *)sender{
CustomTableViewController *nextController = [[CustomTableViewController alloc]init];
[self.navigationController pushViewController:nextController animated:YES];
}
- (void)editAction:(UIBarButtonItem *)sender{
[_tableview setEditing:!_tableview.editing animated:YES];
//三目运算判断是否处于编辑状态,改变标题
self.navigationItem.leftBarButtonItem.title = _tableview.editing ? @"done" : @"edit";
}
//多少个分区
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataSource.count;
}
//返回当前的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//实例化
//可重用机制
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//展示到cell
cell.textLabel.text = _dataSource[indexPath.row];
return cell;
}
/**< 删除 */
//1.配置删除按钮的 title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
//2.提交删除状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//a.删除数据
[_dataSource removeObjectAtIndex:indexPath.row];
//b.移除表格行(带动画效果)
[_tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
/**< 移动 */
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//交换数据源的数据
[_dataSource exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
@end
CustomTableViewController.m
#import "CustomTableViewController.h"
#import "CustomTableViewCell.h"
@interface CustomTableViewController ()
//数据源
@property (nonatomic, strong) NSMutableArray *dataSource;
@end
@implementation CustomTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 70.0;
[self loadDataSource];
}
- (void)loadDataSource{
_dataSource = [NSMutableArray array];
for (NSInteger index = 1; index < 10; index ++) {
NSDictionary *dic = @{@"image":[NSString stringWithFormat:@"fruit%d",index],@"title":[NSString stringWithFormat:@"我是第%d行",index]};
[_dataSource addObject:dic];
}
//刷新表格
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifer = @"customcell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
if (!cell) {
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
}
//获取到数据
NSDictionary *dic = _dataSource[indexPath.row];
cell.logoImageViw.image = [UIImage imageNamed:dic[@"image"]];
cell.titleInfoLabel.text = dic[@"title"];
return cell;
}
//表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40.0;
}
//自定义表头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *aView = [[UIView alloc]init];
aView.backgroundColor = [UIColor cyanColor];
//只要添加自定义控件即可
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(170, 0, 70, 40)];
label.text = @"蔬菜";
[aView addSubview:label];
return aView;
}
//指定每一行 cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//指定第一行的大小
if (indexPath.row == 1) {
return 60.0;
}
return 70.0;
}
@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI{
/**< 图片*/
_logoImageViw = [[UIImageView alloc]initWithFrame:CGRectMake(15.0, 10.0, 50, 50)];
//背景颜色
// _logoImageViw.backgroundColor = [UIColor redColor];
/**< 阴影 */
_logoImageViw.layer.shadowColor = [UIColor blackColor].CGColor;
_logoImageViw.layer.shadowOpacity = 0.5;
_logoImageViw.layer.shadowOffset = CGSizeMake(3, 3);
//加载到self上或者加载到self.contentView上
[self.contentView addSubview:_logoImageViw];
/**< 标题 */
_titleInfoLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_logoImageViw.frame) + 20, 20, 150, 30)];
//_titleInfoLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:_titleInfoLabel];
}
@end