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的简单使用的更多相关文章

  1. swfit-学习笔记(表UITableView的简单使用)

    /*使用与Object-C基本类似,只做简单地使用,创建表及其设置数据源和代理*/ import UIKit class ViewController: UIViewController,UITabl ...

  2. UITableView的简单总结与回顾

    今天突发奇想的想对UItableView做一下汇总,感觉在编程中这个控件可以千变万化也是用的最多的一个了,下面就为大家简单总结下这个控件,也许还有不足,不过还是请各位不吝赐教了哈,那么我就开始了,我会 ...

  3. Swift中UITableView的简单使用

    Swift中的注释 使用"// MARK:- 注释内容",对属性或方法进行注释 使用"///注释内容"对属性或方法提供调用说明的注释 使用extension对同 ...

  4. UITableView的简单应用介绍

    创建一个tableView视图,然后把这个视图界面添加到主界面上. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, [ ...

  5. IOS SWIFT UITableView 实现简单微博列表

    // // Weibo.swift // UITableViewCellExample // // Created by XUYAN on 15/8/15. // Copyright (c) 2015 ...

  6. Swift 2.0 UItableView 的简单使用

    在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方.下面我先把整个控制器的 ...

  7. swift 实践- 01 -- UItableView的简单使用

    import UIKit class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{ over ...

  8. ios初识UITableView及简单用法二(模型数据)

    // // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...

  9. ios初识UITableView及简单用法一

    // // ViewController.m // ZQRTableViewTest // // Created by zzqqrr on 17/8/24. // Copyright (c) 2017 ...

随机推荐

  1. Oracle数据库如何授权收费(Database Licensing)

    Oracle软件本身是免费的,所以任何人都可以从Oracle官方网站下载并安装Oracle的数据库软件,收费的是License,即软件授权,如果数据库用于商业用途,就需要购买相应Oracle产品的Li ...

  2. Parallax Occlusion Mapping

    如上图,本来是采样original texture coordinates点的颜色,其实却采样了correcter texture coordinates点的颜色. 而且会随着视线的不同看到凹凸程度变 ...

  3. ssh 设置免password登录

    如果: 实现->操作机A机 要以用户"user1"身份.免password登录B机和C机? B机和C机 建立用户 user1 而且能够ssh 登录. A机设置: 1.安装ss ...

  4. cmp排序hdoj 1106排序

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下cmp排序 /*标题还是比拟的水吧,但是花的时间还是比拟的多,心不够静*/ #include <iostrea ...

  5. cdoj 1246 每周一题 拆拆拆~ 分解质因数

    拆拆拆~ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1246 Descri ...

  6. tomcat配置虚拟主机

    在眼下,非常多server都是一台server对外能够訪问非常多个javaEE的项目,这样的实现方式就须要在tomcat里面配置虚拟主机了!以下就说说怎样配置虚拟主机: 找到tomcat的安装文件夹, ...

  7. Proteus仿真_01、 8086 IO译码仿真

    最近在学习一些微机原理与接口技术方面的知识. 参考书籍<微机原理与接口技术---基于8086Proteus仿真> 顾晖 梁惺彦 编著 实验一.利用8086 芯片来实现对I/O设备的读取和控 ...

  8. iOS开发——网络编程Swift篇&(六)异步Post方式

    异步Post方式 // MARK: - 异步Post方式 func asynchronousPost() { //创建NSURL对象 var url:NSURL! = NSURL(string: &q ...

  9. Shell脚本调试工具set

    可以使用set命令的x选项,显示所有命令执行及变量值的变化过程等. 具体使用方法:首先使用set -x开启调试模式,最后使用命令set +x关闭调试模式. 一个简单示例演示如何使用set命令进行脚本调 ...

  10. 一天一个mysql函数(一) cast && convert

    MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值.两者具体的语法如下: CAST(value as type); CONVERT(value, type) ...