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 ...
随机推荐
- 自己写一个jQuery垂直滚动栏插件(panel)
html中原生的滚动栏比較难看,所以有些站点,会自己实现滚动栏,导航站点hao123在一个側栏中,就自己定义了垂直滚动栏,效果比較好看,截图例如以下: watermark/2/text/aHR0cDo ...
- Python 删除目录中特定文件
代码如下,使用了递归: import sys currDir = sys.path[] import os def removeFile(dir,postfix): if os.path.isdir( ...
- 2015南阳CCPC C - The Battle of Chibi DP
C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...
- C#操作MySQL数据库-----HelloWorld
这里采用在visual studio 2010中通过MySql.Data.dll.MySql.Web.dll来连接mysql数据库, 之后便进行数据的插入和查询. Program.cs文件内容如下: ...
- MemCached Cache Java Client封装优化历程
1.什么是memcached?(从官网翻译翻译) 免费和开源.高性能.分布式内存对象缓存系统,通用在自然界,但用于加速动态web应用程序,减轻数据库负载. Memcached是一个内存中的键值存储为小 ...
- jQuery Pagination Ajax分页插件中文详解(转)
一.相关demo 基本demo页面 Ajax demo页面 参数可编辑demo页面 二.简介与说明 此jQuery插件为Ajax分页插件,一次性加载,故分页切换时无刷新与延迟,如果数据量较大不建议用此 ...
- RTB广告展示分步说明
原文:http://contest.ipinyou.com/cn/manual.shtml RTB (Real Time Bidding, 实时竞价) 是近年来计算广告领域最激动人心的进展之一. 它增 ...
- HBase shell 常用指令
HBase shell 常用指令 连接HBase $ ./bin/hbase shell 打开帮助 hbase(main):001:0> help 创建表 hbase(main):003:0&g ...
- RPC框架Thrift例子-PHP调用C++后端程序
更新 2016-02-22: Response对象不用主动创建. 前言 前段时间用了一下Facebook的开源RPC框架Thrift,做PHP客户端调用C++后端程序,真心觉得Thrift不错! 本文 ...
- 解决位图失真-SetStretchBltMode()
当用以下函数加载一张位图时,当窗口发生重绘更改大小时,位图将失真: CBitmap bitmap; bitmap.LoadBitmap(IDB_BITMAP2); BITMAP bmp; bitm ...