//
// ViewController.m
// Simple Table
//
// Created by Jierism on 16/7/20.
// Copyright © 2016年 Jierism. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
// 声明一个数组,用来储存表单元的内容
@property(copy,nonatomic) NSArray *dwarves; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 初始化表单元的内容
self.dwarves = @[@"SLEEPY",@"SNEEZY",@"BASHFUL",@"HAPPY",@"DOC",@"GRUMPY",@"DOPEY",@"THORIN",@"DORIN",
@"NORI",@"ORI",@"BALIN",@"DWALIN",@"FILI",@"KILI",@"OIN",@"GLOIN",@"BIFUR",@"BOFUR"];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} // 返回数组的元素个数,即cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dwarves count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ // 声明一个静态字符串实例,作为键使用,用来表示某种表单元
// 比较复杂的表需要根据它们的内容和位置使用不同的类型的表单元,这样就需要不同的表单元标识符来区分每一种表单元类型
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; // 生成表单元
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
// 当视图中没有表单元时,生成表单元,数目为数组的元素个数
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault // 设置cell的样式,这里为默认
reuseIdentifier:SimpleTableIdentifier];
} // 在每个表单元前面插入一个图像
UIImage *image = [UIImage imageNamed:@"star"]; // 普通状态时显示这个
cell.imageView.image = image;
UIImage *highlightedImage = [UIImage imageNamed:@"star2"]; // 被点击时显示这个
cell.imageView.highlightedImage = highlightedImage; cell.textLabel.text = self.dwarves[indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:]; // 改变字体大小 // 设置每行的细节文本,当cell的样式设置为非默认才会显示
if (indexPath.row < ) {
cell.detailTextLabel.text = @"Mr.Disney"; // 前面7行的内容是Mr.Disney
}else{
cell.detailTextLabel.text = @"Mr.Tolkien"; // 后面的内容是 Mr.Tolkien
}
return cell;
} // 设置行单元级缩进
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row % ;
} // 设置行不能被选中,这里指定第一行
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == ) {
return nil;
}else{
return indexPath;
}
} // 警告显示
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *rowValue = self.dwarves[indexPath.row];
NSString *message = [[NSString alloc] initWithFormat:@"You selected %@",rowValue]; UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Row Selected!" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Yes I did" style:UIAlertActionStyleDefault handler:nil]; [controller addAction:cancelAction];
[self presentViewController:controller animated:YES completion:nil];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} // 修改行的高度,这里指定除了第一行是120以外,其他行均为70r
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row == ? :;
} @end

以上代码实现了一个简单的表视图,运行效果如下图

选中一个cell,图标发生变化,并显示警告弹窗,如图

其中这段代码,只有当cell设置为Default以外的任意一种style才会显示

 // 设置每行的细节文本,当cell的样式设置为非默认才会显示
if (indexPath.row < ) {
cell.detailTextLabel.text = @"Mr.Disney"; // 前面7行的内容是Mr.Disney
}else{
cell.detailTextLabel.text = @"Mr.Tolkien"; // 后面的内容是 Mr.Tolkien
}
return cell;

UITableViewCellStyleValueSubtitle运行效果:

(ps:为了能看清楚效果,我把字体改小了)

UITableViewCellStyleValue1运行效果:

UITableViewCellStyleValue2运行效果:

iOS开发-表视图的使用的更多相关文章

  1. IOS开发-表视图LV3导航控制器

    学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...

  2. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  3. iOS开发中视图控制器ViewControllers之间的数据传递

    iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...

  4. IOS开发之视图和视图控制器

    视图(View), 视图控制器(ViewController)是IOS开发UI部分比较重要的东西.在学习视图这一块的东西的时候,感觉和Java Swing中的Panel差不多.在UIKit框架中都有一 ...

  5. IOS之表视图添加搜索栏

    下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的.     1.将Search bar and search display拖动到ViewController中.不要添加Sear ...

  6. iOS开发:视图生命周期

    iOS应用的视图状态分为以下几种 在viewcontroller的父类UIViewController中可以看到如下代码,通过重写不同的方法对操作视图渲染. @available(iOS 2.0, * ...

  7. iOS开发中视图相关的小笔记:push、modal、popover、replace、custom

    在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,segue的类型是不同的. 在iphone中,segue有:push,modal,和custom三种不同的类型, ...

  8. iOS UITableView表视图滚动隐藏UINavigationController导航栏

    UITableView 继承于UIScrollView 所以UIScrollView 的代理方法相同适用于UITableView 中 隐藏导航栏的方法为: self.navigationControl ...

  9. IOS之表视图单元格删除、移动及插入

    1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...

随机推荐

  1. Windows 8 自带定时关机的4种实现方法

    问题描述:前几天发布了一篇文章[ Windows 7/8 自带定时关机命令 ],文章中的用到的命令我在Windows 7都运行成功,但没有在Windows 8 上进行测试,因为我认为Windows 8 ...

  2. 宏HASH_INSERT

    调用 方法 HASH_INSERT(lock_t, hash, lock_sys->rec_hash,lock_rec_fold(space, page_no), lock); /******* ...

  3. 记一次Sql优化过程

    这几天在写一个存储过程,反复优化了几次,从最开始的7分钟左右,优化到最后的几秒,并且这个过程中我的导师帮我指点了很多问题,这些指点都是非常宝贵的,独乐乐不如众乐乐,一起来分享这次的优化过程吧. 这个存 ...

  4. light开发框架

  5. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  6. Mysql加密方式

    MySQL数据库的认证密码有两种方式, MySQL 4.1版本之前是MySQL323加密,MySQL 4.1和之后的版本都是MySQLSHA1加密, MySQL数据库中自带Old_Password(s ...

  7. C++学习笔记:指向函数的指针

    #include <stdio.h> int sum(int a, int b) { return a+b; } int minus(int a, int b) { return a-b; ...

  8. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.8

    Prove that for any matrices $A,B$ we have $$\bex |\per (AB)|^2\leq \per (AA^*)\cdot \per (B^*B). \ee ...

  9. Initializing nested object properties z

    public class Employee { public Employee() { this.Insurance = new Insurance(); } // Perhaps another c ...

  10. linux下Qt问题cannot find -lGL collect2: error: ld returned 1 exit status

    fedora下解决 yum groupinstall "Development Tools" yum install mesa-libGL-devel ubuntu下解决 sudo ...