iOS开发UI篇—UITableview控件基本使

iOS开发UI篇—UITableview控件基本使用

一、一个简单的英雄展示程序

NJHero.h文件代码(字典转模型)

#import <Foundation/Foundation.h>

@interface NJHero : NSObject
/**
 *  头像
 */
@property (nonatomic, copy) NSString *icon;
/**
 *  名称
 */
@property (nonatomic, copy) NSString *name;
/**
 *  描述
 */
@property (nonatomic, copy) NSString *intro;

- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)heroWithDict:(NSDictionary *)dict;
@end

NJViewController.m文件代码

#import "NJViewController.h"
#import "NJHero.h"

@interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
 *  保存所有的英雄数据
 */
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation NJViewController

#pragma mark - 懒加载
- (NSArray *)heros
{
    if (_heros == nil) {
        // 1.获得全路径
        NSString *fullPath =  [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
        // 2.更具全路径加载数据
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
        // 3.字典转模型
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
        for (NSDictionary *dict in dictArray) {
            NJHero *hero = [NJHero heroWithDict:dict];
            [models addObject:hero];
        }
        // 4.赋值数据
        _heros = [models copy];
    }
    // 4.返回数据
    return _heros;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置Cell的高度
    // 当每一行的cell高度一致的时候使用属性设置cell的高度
    self.tableView.rowHeight = 60;
    self.tableView.delegate = self;
}

#pragma mark - UITableViewDataSource
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 返回每一组有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.heros.count;
}
// 返回哪一组的哪一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建CELL
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    // 2.设置数据
    // 2.1取出对应行的模型
    NJHero *hero = self.heros[indexPath.row];
    // 2.2赋值对应的数据
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    // 3.返回cell
    return cell;
}
#pragma mark - UITableViewDelegate
/*
// 当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1 == indexPath.row) {
        return 180;
    }
    return 44;
}
 */

#pragma mark - 控制状态栏是否显示
/**
 *   返回YES代表隐藏状态栏, NO相反
 */
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end

实现效果:

代码注意点:

(1)在字典转模型的代码处用下面的代码,为可变数组分配dictArray.count个存储空间,可以提高程序的性能

NSMutableArray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];

(2)设置cell的高度

有三种办法可以设置cell的高度

1) 可以在初始加载方法中设置,self.tableView.rowHeight = 60;这适用于当每一行的cell高度一致的时候,使用属性设置cell的高度。

2)在storyboard中设置,适用于高度一致

3)当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (1 == indexPath.row) {

return 180;

}

return 44;

}

二、cell的一些属性

代码示例:

#import "NJViewController.h"
#import "NJHero.h"

@interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
 *  保存所有的英雄数据
 */
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation NJViewController

#pragma mark - 懒加载
- (NSArray *)heros
{
    if (_heros == nil) {
        // 1.获得全路径
        NSString *fullPath =  [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
        // 2.更具全路径加载数据
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
        // 3.字典转模型
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
        for (NSDictionary *dict in dictArray) {
            NJHero *hero = [NJHero heroWithDict:dict];
            [models addObject:hero];
        }
        // 4.赋值数据
        _heros = [models copy];
    }
    // 4.返回数据
    return _heros;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置Cell的高度
    // 当每一行的cell高度一致的时候使用属性设置cell的高度
    self.tableView.rowHeight = 60;
    self.tableView.delegate = self;

}

#pragma mark - UITableViewDataSource
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 返回每一组有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.heros.count;
}
// 返回哪一组的哪一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建CELL
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    // 2.设置数据
    // 2.1取出对应行的模型
    NJHero *hero = self.heros[indexPath.row];
    // 2.2赋值对应的数据
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];

    // 2.3设置cell的辅助视图
    // cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    if (0 == indexPath.row) {
        cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    }else
    {
        cell.accessoryView = [[UISwitch alloc] init];
    }
//    UIButton *btn = [[UIButton alloc] init];
//    btn.backgroundColor = [UIColor redColor];
//    cell.accessoryView = btn;

    // 2.4设置cell的背景颜色
    cell.backgroundColor = [UIColor blueColor];

    // 设置默认状态的背景
//    UIView *view = [[UIView alloc] init];
//    view.backgroundColor = [UIColor blueColor];
//    cell.backgroundView = view;

    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]];
    cell.backgroundView = iv;

    // 设置选中状态的背景
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor purpleColor];
    cell.selectedBackgroundView = view2;
    // 3.返回cell
    return cell;
}

#pragma mark - 控制状态栏是否显示
/**
 *   返回YES代表隐藏状态栏, NO相反
 */
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end

实现效果:

cell的一些属性:

(1)设置cell的辅助视图,设置cell.accessoryView(系统提供了枚举型,也可以自定义@父类指针指向子类对象);

(2)设置cell的背景颜色,有两种方式可以设置cell的背景颜色:

通过backgroundColor 和 backgroundView都可以设置cell的背景。但是backgroundView 的优先级比 backgroundColor的高,所以如果同时设置了backgroundColor和backgroundView, 那么backgroundView会盖住backgroundColor

示例:cell.backgroundColor = [UIColorblueColor];

(3)设置cell默认状态的背景

示例1:

      UIView *view = [[UIView alloc] init];

   view.backgroundColor = [UIColor blueColor];

  cell.backgroundView = view;

示例2:

UIImageView *iv = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"buttondelete"]];

cell.backgroundView = iv;(父类指针指向子类对象,可以使用图片用简单的操作设置绚丽的效果)

(4)设置cell选中状态的背景

示例:

  UIView *view2 = [[UIView alloc] init];

view2.backgroundColor = [UIColorpurpleColor];

cell.selectedBackgroundView = view2;

三、tableview的一些属性

代码示例:

#import "NJViewController.h"

@interface NJViewController ()<UITableViewDataSource>

@end

@implementation NJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 1.创建tableview
    UITableView *tableview = [[UITableView alloc] init];
    tableview.frame = self.view.bounds;

    // 2.设置数据源
    tableview.dataSource =self;

    // 3.添加tableview到view
    [self.view addSubview:tableview];

    // 4.设置分割线样式
    // tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 5.设置分割线颜色
     接收的参数是颜色的比例值
    tableview.separatorColor = [UIColor colorWithRed:0/255.0 green:255/255.0 blue:0/255.0 alpha:255/255.0];

    // 设置tableview的头部视图
    tableview.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    tableview.tableFooterView = [[UISwitch alloc] init];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    // 2.设置cell的数据
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row ];

    // 3.返回cell
    return cell;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end

实现效果:

tableview的一些属性:

(1)设置分割样式(tableview.separatorStyle),这是个枚举类型

(2)设置分割线的颜色,可以直接使用系统给出的颜色,如果系统给定的颜色不能满足需求时,也可以自定义。

  补充:颜色分为24位和32位的,如下

24bit颜色

R 8bit 0 ~ 255

G 8bit 0 ~ 255

B 8bit 0 ~ 255

32bit颜色

A 8bit 0 ~ 255(tou)

R 8bit

G 8bit

B 8bit

#ff ff ff 白色

#00 00 00 黑色

#ff 00 00 红色

#255 00 00

设置为自定义颜色的实例:tableview.separatorColor = [UIColorcolorWithRed:0/255.0green:255/255.0blue:0/255.0alpha:255/255.0];

//接收的参数是颜色的比例值

(3)设置顶部和底部视图

tableview.tableHeaderView   //顶部

tableview.tableFooterView    //底部

时间: 2024-08-01 22:40:30

iOS开发UI篇—UITableview控件基本使的相关文章

iOS开发UI篇—UITableview控件使用小结

iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 2.告诉每组一共有多少行 方法:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege

iOS开发UI篇—UITableview控件简单介绍

一.基本介绍 在众多移动应?用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支持垂直滚动,?且性能极佳 . UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置. 二.数据展示 UITableView需要?一个数据源(dataSource)来显示数据UITableView会向数据源查询一共有多少行数据以及每?行显示什么数据等 没有设置数据源

iOS开发UI篇—UITableview控件基本使用

一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 1 #import <Foundation/Foundation.h> 2 3 @interface NJHero : NSObject 4 /** 5 * 头像 6 */ 7 @property (nonatomic, copy) NSString *icon; 8 /** 9 * 名称 10 */ 11 @property (nonatomic, copy) NSString *name; 12 /** 13 * 描述 1

iOS开发UI篇—UIPickerView控件简单介绍

iOS开发UI篇—UIPickerView控件简单介绍 一.UIPickerView 控件 1.简单介绍: 2.示例代码 TXViewController.m文件 1 // Created by 鑫 on 14-10-15. 2 3 // Copyright (c) 2014年 梁镋鑫. All rights reserved. 4 5 // 6 7 8 9 #import "TXViewController.h" 10 11 12 13 @interface TXViewContro

【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: 1 #import "YYViewController.h" 2 3 @interface YYViewController () <UIScrollViewDelegate> 4 @property (w

OS开发UI篇—UITableview控件使用小结

一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 2.告诉每组一共有多少行 方法:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 3.设置每组每行(cell) 方

iOS开发UI篇—UIScrollView控件实现图片缩放功能

一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理.也就是说,要完成缩放功能的话,只需要将需要缩放的内容添加到UIScrollView中 2.缩放原理 当用户在UIScrollView身上使用捏合手势时,UIScrollView会给代理发送一条消息,询问代理究竟要缩放自己内部的哪一个子控件(哪一块内容) 当用户在UIScrollView身上使用捏合手势时,UIScrollView会调用代理的v

iOS开发UI篇—UIScrollView控件介绍

一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕?大?小是极其有限的,因此直接展?示在?用户眼前的内容也相当有限 (2)当展?示的内容较多,超出?一个屏幕时,?用户可通过滚动?手势来查看屏幕以外的内容 (3)普通的UIView不具备滚动功能,不能显?示过多的内容 (4)UIScrollView是一个能够滚动的视图控件,可以?用来展?示?大量的内容,并且可以通过滚 动查看所有的内容 (5)  举例:手机上的“设置”.其他?示例程序 2.UIScrollView的简

iOS开发UI篇—UIScrollView控件实现图片轮播

一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: 1 #import "YYViewController.h" 2 3 @interface YYViewController () <UIScrollViewDelegate> 4 @property (weak, nonatomic) IBOutlet UIScrollView *scrollview; 5 /** 6 * 页码 7 */ 8 @property (w