UITableView的简单使用
UITableView分为两种style:UITableViewStyleGrouped和UITableViewStylePlain。
(一)UITableViewStyleGrouped
#import "ViewController.h"
//行高
#define cellHeight 60
//边距
#define margin 10
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *myTableView;
NSArray *myData;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; myTableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
myTableView.dataSource = self;
myTableView.delegate = self;
//默认分隔线设置为无
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:myTableView];
[self initWithData];
}
- (void)initWithData
{
myData = @[
@{
@"city":@"北京",
@"cityfooter":@"我是北京",
@"district":@[@"朝阳区",@"海淀区"],
@"districtdetail":@[@"我是朝阳区",@"我是海淀区"],
@"image":@[@"",@""],
},
@{
@"city":@"上海",
@"cityfooter":@"我是上海",
@"district":@[@"徐汇区",@"闵行区",@"浦东新区"],
@"districtdetail":@[@"我是徐汇区",@"我是闵行区",@"我是浦东新区"],
@"image":@[@"",@"",@""],
},
];
} #pragma mark --------------tableviewDataSource---------------
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return myData.count;
}
//每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[myData[section] objectForKey:@"district"] count];
}
//组头
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [myData[section] objectForKey:@"city"];
}
//组尾
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [myData[section] objectForKey:@"cityfooter"];
}
//设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellString = @"cellString";//cell的重用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString]; UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, margin, cellHeight-margin*, cellHeight-margin*)];
cellImageView.backgroundColor = [UIColor brownColor];
cellImageView.tag = ;
[cell addSubview:cellImageView]; UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, margin, self.view.frame.size.width-(CGRectGetMaxX(cellImageView.frame)+margin), )];
cellText.tag = ;
[cell addSubview:cellText]; UILabel *cellDetailText = [[UILabel alloc]initWithFrame:CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, )];
cellDetailText.textColor = [UIColor lightGrayColor];
cellDetailText.tag = ;
[cell addSubview:cellDetailText];
//设置点选效果为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置行右边的箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//图标
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:];
cellImageView.image = [UIImage imageNamed:[myData[indexPath.section] objectForKey:@"image"][indexPath.row]]; //标题
UILabel *cellText = (UILabel *)[cell viewWithTag:];
cellText.text = [myData[indexPath.section] objectForKey:@"district"][indexPath.row];
[self getHeightWithLabel:cellText andFontSize:]; //副标题
UILabel *cellDetailText = (UILabel *)[cell viewWithTag:];
cellDetailText.text = [myData[indexPath.section] objectForKey:@"districtdetail"][indexPath.row];
cellDetailText.frame = CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, );
[self getHeightWithLabel:cellDetailText andFontSize:]; //分隔线
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, cellHeight-, self.view.frame.size.width, )];
[lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
[cell addSubview:lineImageView]; return cell;
}
#pragma mark ---------------tableviewDelegate------------------
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cellHeight;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//这里写点击方法
NSLog(@"点击了第%li组第%li行",(long)indexPath.section,(long)indexPath.row);
}
//UIlabel自适应高
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = ;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*, ) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect;
label.attributedText = labelString;
}
(二)UITableViewStylePlain
#import "ViewController.h"
//行高
#define cellHeight 60
//边距
#define margin 10
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *myTableView;
NSArray *myData;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; myTableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
myTableView.dataSource = self;
myTableView.delegate = self;
//默认分隔线设置为无
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:myTableView];
[self initWithData];
}
- (void)initWithData
{
//此数组格式类似于网络开发用到过的Json格式
myData = @[
@{
@"city":@"北京",
@"district":@"朝阳区",
@"districtdetail":@"我是朝阳区",
@"image":@"",
},
@{
@"city":@"北京",
@"district":@"海淀区",
@"districtdetail":@"我是海淀区",
@"image":@"",
},
@{
@"city":@"上海",
@"district":@"徐汇区",
@"districtdetail":@"我是徐汇区",
@"image":@"",
},
@{
@"city":@"上海",
@"district":@"闵行区",
@"districtdetail":@"我是闵行区",
@"image":@"",
},
@{
@"city":@"上海",
@"district":@"浦东新区",
@"districtdetail":@"我是浦东新区",
@"image":@"",
},
];
} #pragma mark --------------tableviewDataSource---------------
//组数(UITableViewStylePlain样式默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
//设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellString = @"cellString";//cell的重用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString]; UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, margin, cellHeight-margin*, cellHeight-margin*)];
cellImageView.backgroundColor = [UIColor brownColor];
cellImageView.tag = ;
[cell addSubview:cellImageView]; UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, margin, self.view.frame.size.width-(CGRectGetMaxX(cellImageView.frame)+margin), )];
cellText.tag = ;
[cell addSubview:cellText]; UILabel *cellDetailText = [[UILabel alloc]initWithFrame:CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, )];
cellDetailText.textColor = [UIColor lightGrayColor];
cellDetailText.tag = ;
[cell addSubview:cellDetailText]; UILabel *cellIntroduceText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellDetailText.frame)+margin, (cellHeight-)/, self.view.frame.size.width-(CGRectGetMaxX(cellDetailText.frame)+margin), )];
cellIntroduceText.textColor = [UIColor darkGrayColor];
cellIntroduceText.tag = ;
[cell addSubview:cellIntroduceText];
//设置点选效果为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置行右边的箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//图标
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:];
cellImageView.image = [UIImage imageNamed:[myData[indexPath.row] objectForKey:@"image"]]; //标题
UILabel *cellText = (UILabel *)[cell viewWithTag:];
cellText.text = [myData[indexPath.row] objectForKey:@"city"];
[self getHeightWithLabel:cellText andFontSize:]; //副标题
UILabel *cellDetailText = (UILabel *)[cell viewWithTag:];
cellDetailText.text = [myData[indexPath.row] objectForKey:@"district"];
cellDetailText.frame = CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, );
[self getHeightWithLabel:cellDetailText andFontSize:];
[self getWidthWithLabel:cellDetailText andFontSize:]; //介绍信息
UILabel *cellIntroduceText = (UILabel *)[cell viewWithTag:];
cellIntroduceText.text = [myData[indexPath.row] objectForKey:@"districtdetail"];
cellIntroduceText.frame = CGRectMake(CGRectGetMaxX(cellDetailText.frame)+margin, (cellHeight-)/, self.view.frame.size.width-(CGRectGetMaxX(cellDetailText.frame)+margin), ); //分隔线
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, cellHeight-, self.view.frame.size.width, )];
[lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
[cell addSubview:lineImageView]; return cell;
}
#pragma mark ---------------tableviewDelegate------------------
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cellHeight;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//这里写点击方法
NSLog(@"点击了第%li行",(long)indexPath.row);
}
//UIlabel自适应高
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = ;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*, ) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect;
label.attributedText = labelString;
}
//UIlabel自适应宽
- (void)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = ;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.width = labelStringRect.size.width;
label.frame = labelRect;
label.attributedText = labelString;
}
UITableView的简单使用的更多相关文章
- swfit-学习笔记(表UITableView的简单使用)
/*使用与Object-C基本类似,只做简单地使用,创建表及其设置数据源和代理*/ import UIKit class ViewController: UIViewController,UITabl ...
- UITableView的简单总结与回顾
今天突发奇想的想对UItableView做一下汇总,感觉在编程中这个控件可以千变万化也是用的最多的一个了,下面就为大家简单总结下这个控件,也许还有不足,不过还是请各位不吝赐教了哈,那么我就开始了,我会 ...
- Swift中UITableView的简单使用
Swift中的注释 使用"// MARK:- 注释内容",对属性或方法进行注释 使用"///注释内容"对属性或方法提供调用说明的注释 使用extension对同 ...
- UITableView的简单应用介绍
创建一个tableView视图,然后把这个视图界面添加到主界面上. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, [ ...
- IOS SWIFT UITableView 实现简单微博列表
// // Weibo.swift // UITableViewCellExample // // Created by XUYAN on 15/8/15. // Copyright (c) 2015 ...
- Swift 2.0 UItableView 的简单使用
在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方.下面我先把整个控制器的 ...
- swift 实践- 01 -- UItableView的简单使用
import UIKit class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{ over ...
- ios初识UITableView及简单用法二(模型数据)
// // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...
- ios初识UITableView及简单用法一
// // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...
随机推荐
- BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法
1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lyds ...
- UVA 1557 - Calendar Game(博弈dp)
UVA 1557 - Calendar Game 题目链接 题意:给定一个日期,两个人轮流走,每次能够走一月或者一天,问最后谁能走到2001.11.4这个日子 思路:记忆化搜索,对于每一个日期,假设下 ...
- DELPHI 多线程
效果不正确 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Control ...
- Android开发 将数据保存到SD卡
前言: 使用Activity的openFileOutput()方法保存文件,文件是存放在手机空间上,一般手机的存储空间不是很大,存放些小文件还行,如果要存放像视频这样的大文件,是不可行的.对于像视频这 ...
- Python2.7.3移除字符串中重复字符(一)
移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...
- Angular 1.2.27在IE7下的兼容问题
最近负责公司的一个国外项目,老外指定要用angular,并且要兼容到IE7. 项目使用的是Angular版本是1.2.27,为了能在IE7下跑,需要做如下配置 1. 加载json2.js 2. 加载h ...
- Mac下使用Fiddler
Fiddler是用C#开发的. 所以Fiddler不能在Mac系统中运行. 没办法直接用Fiddler来截获MAC系统中的HTTP/HTTPS, Mac 用户怎么办呢? Fiddler可以允 ...
- CentOS中查看系统资源占用情况的命令
用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...
- vim中taglist使用
转载:http://www.cnblogs.com/mo-beifeng/archive/2011/11/22/2259356.html 本节所用命令的帮助入口: :help helptags :he ...
- python之装饰器详解
这几天翻看python语法,看到装饰器这里着实卡了一阵,最初认为也就是个函数指针的用法,但仔细研究后发现,不止这么简单. 首先很多资料将装饰器定义为AOP的范畴,也就是Aspect Oriented ...