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. SQL拆分多规则的字符串分离数字。

    --拆分多规则字符串 DECLARE @Codes NVARCHAR(MAX) SET @Codes = '6*BC-007,*BC-016,9*BC-015' --对于*BC-015这种情况,则Qt ...

  2. HTML5 - HTML5 postMessage API 注意事项

    一:发送 window.postMessage("Hello, world", "http://127.0.0.1:8080"); 注意,必须要加上http:/ ...

  3. ListView为什么用setOnItemClick这个方法和onTouch这个方法有冲突

    因为如果onTouch方法中返回true的话,这次事件就被ListView中的item控件消费了,所以不会执行ListVIew的setOnItemClick这个方法了,如果onTouch方法返回fal ...

  4. Klist

    显示当前缓存的 Kerberos 票证的列表. 有关如何使用此命令的示例 语法 klist [-<LogonId.HighPart> lh] [-li <LogonId.LowPar ...

  5. mac下批量删除.svn文件

    mac下.svn是隐藏文件,而且即使我们调成可见的,一个一个删也很麻烦.今天正好同事问起来这个命令,于是想可能有些人也需要,于是还是放到博客里吧 命令比较简单,其实就是一条linux命令,打开终端,首 ...

  6. Codeforces Gym 100733H Designation in the Mafia flyod

    Designation in the MafiaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...

  7. ireport启动闪退问题

    安装好ireport之后,双击ireport.exe启动程序只是掠过启动画面便毫无反应, 后来在网上找了下解决方法,才知道只因为ireport与jdk8不兼容, 于是下载了jdk6,并在ireport ...

  8. 文件I/O(不带缓冲)之文件共享

    UNIX系统支持在不同进程间共享打开的文件. 内核使用三种数据结构表示打开的文件,它们之间的关系决定了在文件共享方面一个进程对另一个进程可能产生的影响. (1)每个进程在进程表中都有一个记录项,记录项 ...

  9. spring源码分析之spring-web web模块分析

    0 概述 spring-web的web模块是更高一层的抽象,它封装了快速开发spring-web需要的基础组件.其结构如下: 1. 初始化Initializer部分 1.1  Servlet3.0 的 ...

  10. PHP生成条形码

    前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...