iOS开发系列 ---- UI (自定义TableView)
来源:互联网 发布:淘宝网纸杯蛋糕 编辑:程序博客网 时间:2024/05/18 20:11
本章实现效果:
我们使用MVC架构来实现自定义TableView,具体的MVC介绍请自行百度,在本章中不作为主要内容讲解。
Model层:
DataSource.h
#import <Foundation/Foundation.h>@interface DataSource : NSObject+ (NSArray *)getUserInfo;@end
DataSource.m
#import "DataSource.h"@implementation DataSource+ (NSArray *)getUserInfo { NSString * path = [[NSBundle mainBundle] pathForResource:@"users" ofType:@"plist"]; NSArray * arrayReturn = [NSArray arrayWithContentsOfFile:path]; return arrayReturn;}@end
View层:
CustomTableViewCell.h
#import <UIKit/UIKit.h>@interface CustomTableViewCell : UITableViewCell@property (nonatomic, strong) UILabel * labelName;//姓名@property (nonatomic, strong) UILabel * labelNum; //手机号@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"@implementation CustomTableViewCell//重写初始化方法:将控件添加到单元格上,如果将子视图控件添加到cell上 借助contenView视图,这样的话cell上子视图会随着cell的变化而变化- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.labelName = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, self.bounds.size.width-200, 40)]; self.labelName.textColor = [UIColor cyanColor]; self.labelName.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:self.labelName]; self.labelNum = [[UILabel alloc] initWithFrame:CGRectMake(100, 40, self.bounds.size.width-200, 40)]; self.labelNum.textColor = [UIColor greenColor]; self.labelNum.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:self.labelNum]; } return self;}- (void)awakeFromNib { [super awakeFromNib];}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated];}@end
ViewController层:
ViewController.m
#import "ViewController.h"#import "DataSource.h"#import "CustomTableViewCell.h"#import "TwoViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>@property (nonatomic, strong) NSMutableArray *arrayDS;@property (nonatomic, strong) UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self setupDatas]; [self setupSubviews];}- (void)setupDatas { //从Model层拿到数据 NSArray * array = [DataSource getUserInfo]; //把拿到的数据给我们的数据源 self.arrayDS = [[NSMutableArray alloc] initWithArray:array]; self.navigationItem.title = @"自定义单元格";}- (void)setupSubviews { self.automaticallyAdjustsScrollViewInsets = NO; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-50, self.view.bounds.size.width, 50)]; view.backgroundColor = [UIColor redColor]; [self.view insertSubview:view aboveSubview:self.tableView];}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arrayDS.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * str = nil; if ([[[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"phoneNum"] hasPrefix:@"100"]) { str = @"CustomCell"; CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str]; if (cell == nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; } cell.labelName.text = [[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"personName"]; cell.labelNum.text = [[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"phoneNum"]; return cell; } else { str = @"SystemCell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; } cell.textLabel.text = [[self.arrayDS objectAtIndex:indexPath.row] objectForKey:@"phoneNum"]; return cell; }}- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView reloadData];}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TwoViewController *twoVC = [[TwoViewController alloc] init]; [self.navigationController pushViewController:twoVC animated:YES];}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100;}@end
自定义表格视图demo(内含plist文件)
1 0
- iOS开发系列 ---- UI (自定义TableView)
- iOS开发系列----UI(简单TableView的使用)
- 【iOS开发系列】tableView详解
- iOS开发系列 ---- UI (TableView的编辑和多项删除操作)
- IOS系列——自定义tableVIew
- 【iOS开发系列】tableView滚动淡出效果
- 【iOS开发TableView】TabelView自定义cell
- iOS开发之代码调用自定义tableView
- 6.26 iOS开发自定义tableView使用
- IOS UI tableView 自定义cell的内容不显示
- [ios] UI TableView
- iOS开发 UI 自定义视图
- IOS开发UI:自定义TabBar
- iOS开发UI篇之tableView 的用法详解
- iOS开发 - 第02篇 - UI进阶 - 02 - TableView
- iOS开发 - 第02篇 - UI进阶 - 03 - TableView Cell
- IOS开发系列--TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式),ios7tableview索引
- iOS tableview viewForHeaderInSection 自定义
- 一个BDoc问题的解决思路
- Arrays.toString() 的用法
- jdk环境的安装与配置
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant
- Revit二次开发--工具下载
- iOS开发系列 ---- UI (自定义TableView)
- qt基础
- docker服务器的图形显示方案
- 链表删除节点
- 浅谈dedecms模板引擎工作原理及自定义标签
- css盒子模型之定位
- 百度笔试-不等式排列
- Ubuntu 14.04配置虚拟主机
- JavaWeb 入门级项目实战 -- 文章发布系统 (第十一节)