ios tableView那些事(三)给tableView添加些图片

article/2025/8/26 11:24:04

感觉光有数据的tableView很丑,那么我们就来美化下吧,加一些图片

#import <UIKit/UIKit.h>

 

/*tableview 一定要用到这两个delegate  UITableViewDataSource,UITableViewDelegate */

@interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>

{

    UITableView *tableview;

    NSArray *array;

    NSArray *arrayImage;

   NSArray *arrayImage1;

    

}

@property (strong,nonatomic)UITableView *tableview;

@property (strong,nonatomic)NSArray *array;

@property (strong,nonatomic)NSArray *arrayImage;

@property (strong,nonatomic)NSArray *arrayImage1;

@end

#import "ViewController.h"


@interfaceViewController ()


@end


@implementation ViewController

@synthesize tableview;

@synthesize array;

@synthesize arrayImage;

@synthesize arrayImage1;

- (void)viewDidLoad

{

    [superviewDidLoad];

tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStyleGrouped];

    

//    UITableViewStylePlain,                

//    UITableViewStyleGrouped

  

    tableview.delegate =self;//不要忘写了这两句话哟调用delegate*/

    tableview.dataSource=self;


  /*改变 UITableViewStyleGrouped 样式的 背景色 */

   tableview.backgroundView = [[UIViewalloc]init];

   tableview.backgroundColor = [UIColorclearColor];

   

    [self.viewaddSubview:tableview];

    

    NSMutableArray *arrayValue = [[NSMutableArrayalloc]init];

    NSMutableArray *arrayImageValue = [[NSMutableArrayalloc]init];

    NSMutableArray *arrayImageValue2 = [[NSMutableArrayalloc]init];

    for (int i = 1; i<= 5; i++)

    {

        NSString *value = [NSStringstringWithFormat:@"%d",i];

        NSString *imageName = [NSStringstringWithFormat:@"image%@.png",value];

        UIImage *image = [UIImageimageNamed:imageName];

        NSLog(@"imageName == %@",imageName);

        [arrayValue addObject:value];

        [arrayImageValue addObject:image];

    }

    


    for (int i = 6;i<=10; i++ )

    {

        NSString *value = [NSStringstringWithFormat:@"%d",i];

        NSString *imageName = [NSStringstringWithFormat:@"image%@.png",value];

        UIImage *image = [UIImageimageNamed:imageName];

        [arrayImageValue2 addObject:image];


    }

   array = arrayValue;

   arrayImage = arrayImageValue;

   arrayImage1 =arrayImageValue2;

}


/* 这个函数是显示tableview的章节数*/

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    return 2;

}

/* 这个函数是指定显示多少cells*/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [arraycount];//这个是指定加载数据的多少即显示多少个cell,如过这个地方弄错了会崩溃的哟

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //定义个静态字符串为了防止与其他类的tableivew重复

    static NSString *CellIdentifier =@"Cell";  

    //定义cell的复用性当处理大量数据时减少内存开销

    UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];

    

    if (cell ==nil)

    {

               cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

    

    if (indexPath.section == 0)

    {

                cell.imageView.image = [arrayImageobjectAtIndex:[indexPathrow]]; 

    }

    else

    {

              cell.imageView.image = [arrayImage1objectAtIndex:[indexPathrow]];

    }

    cell.backgroundColor = [UIColorgrayColor];

    

    cell.detailTextLabel.text = [arrayobjectAtIndex:[indexPathrow]];

    

    cell.textLabel.text  =  [arrayobjectAtIndex:[indexPathrow]];

    

    return cell;

}



- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

是不是比以前好多了





有些时候我们不想要那个方框或者定义的时候想换成自己的背景图片

那么我们就在下面的函数里加两句代码吧


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    

    //定义个静态字符串为了防止与其他类的tableivew重复

    static NSString *CellIdentifier = @"Cell";  

    //定义cell的复用性当处理大量数据时减少内存开销

    UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];

    

    if (cell ==nil)

    {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

    

    if (indexPath.section == 0)

    {

       

        cell.imageView.image = [arrayImageobjectAtIndex:[indexPathrow]];

     

    }

    else

    {

        cell.imageView.image = [arrayImage1objectAtIndex:[indexPathrow]];


    }

    

    /*去掉方框 ,或者把view 换成imageview */

    UIView *backView = [[UIViewalloc]init];

   [cell setBackgroundView:backView];

    cell.backgroundColor = [UIColorclearColor];


    

    cell.detailTextLabel.text = [arrayobjectAtIndex:[indexPathrow]];

    

    cell.textLabel.text  =  [arrayobjectAtIndex:[indexPathrow]];

    

    return cell;

}

看下效果吧


现在我们把代码改成  UITableViewStylePlain 样式的在加些处理

我们给tableview换个背景图片吧!我们在

- (void)viewDidLoad 函数里加上这几行代码吧

  {

    UIImage *backImageName= [UIImageimageNamed:@"iOS 7 wall 3.png"];

    UIImageView *taleviewBackImageView = [[UIImageViewalloc]initWithImage:backImageName];

    [self.tableviewsetBackgroundView:taleviewBackImageView];


}


这回我们看下效果是不是更好了!


当我们选中tableview 的时候会发现是蓝色背景的!这个是有几个属性的我在这里就先不介绍了!在玩一些应用的时候我们会发现点击列表的时候会改变颜色,表示你选中的那个cell ,其实是都是一些图片构成的!

现在我添加一个跟cell一样宽的图片!


我们只需要在下面的函数里加上两行代码就ok了效果如下图


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   

    UIImageView *cellimageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"nav_on.png"]];

    cell.selectedBackgroundView = cellimageView;

    

}


下面是系统默认的点击颜色

//无色  

cell.selectionStyle = UITableViewCellSelectionStyleNone;  

//蓝色 ,系统默认是蓝色的 

cell.selectionStyle = UITableViewCellSelectionStyleBlue;  

//灰色  

cell.selectionStyle = UITableViewCellSelectionStyleGray;









http://chatgpt.dhexx.cn/article/QpQXrtEc.shtml

相关文章

QT的TableView实现多级表头

QT的TableView实现多级表头 最近项目需要支持多级表头&#xff0c;QT本身不支持。可重写QHeaderView表头实现。 demo如下&#xff1a; FSItemDelegate.h #pragma once/* 自定义委托类 */ #include <QItemDelegate> #include <QSpinBox> #include <QDoubleSpin…

QML类型:TableView

一、描述 TableView 显示从内置 QML 类型&#xff08;如 ListModel 和 XmlListModel&#xff09;创建的模型中的数据&#xff0c;这些模型仅填充 TableView 中的第一列。要创建具有多列的模型&#xff0c;请使用 TableModel 或继承 QAbstractItemModel 的 C 模型。 TableView…

QML TableView表格使用示例

前言 QML中实现表格可以使用多种方式&#xff0c;比如直接使用ListView&#xff0c;定义每一行delegate&#xff0c;或者自定义Rectangle&#xff0c;放到Flipable中组合使用。Qt Quick Control1中 从5.1版本开始就提供了表格控件&#xff0c;但是感觉不怎么好用&#xff0c;在…

Qt TableView的简单使用

软件环境&#xff1a; ubuntu -------------------------------------------------------------------------------------------------------- 最终效果图&#xff1a; --------------------------------------------------------------------------------------------------…

PyQt5 TableView组件

一、话不多说&#xff0c;先看图 本次要实现的是主窗口内添加widget组件&#xff0c;widget内设置成垂直盒布局&#xff0c;然后在布局中添加tableView、PushButton组件 二、看main函数 if __name__ __main__:app QApplication(sys.argv)# 现在这创建 主窗口 &#xff08;不然…

优雅的开发TableView

前言 UITableView&#xff08;UITableViewController&#xff09;是iOS开发使用频率最高的一个组件。 不管是使用UITableView还是还是UITableViewController&#xff0c;在开发的时候&#xff0c;我们都需要实现两个协议&#xff1a; UITableViewControllerDataSourceUITabl…

JavaFX控件——TableView

在JavaFX 应用中对创建表格最重要的是TableView, TableColumn和TableCell这三个类。 你可以通过实现数据模型&#xff08;data model&#xff09; 和 实现 单元格工厂(cell factory) 来填充表格。 表格类提供了表格列嵌入式的排序能力和必要时调整列宽度的功能。 下面开始学…

ios开发:多个Section的TableView

开发多个Section的tableView。 首先应该考虑到数据源该如何得到 我们这里可以通过两种方式:第一种是读取plist文件。第二种是通过代码进行数据存储以及读取。 多个Section需要的数据源是一个字典&#xff0c;字典里的内容是一个数组。在plist文件中可以这样去创建 在.h文件中…

tableview概述

转自&#xff1a;http://www.cnblogs.com/smileEvday/archive/2012/06/28/tableView.html                 下面分9个方面进行介绍&#xff1a; 一、UITableView概述 UITableView继承自UIScrollView&#xff0c;可以表现为Plain和Grouped两种风格&#xff0c;分…

ios tableView那些事(一)创建一个简单的tableView

工作也有半年多了&#xff01;几乎每个项目中的会用到tableview这个神奇而好用的控件&#xff0c;在学习和工作中都会看别人的博客&#xff01;对我有很大的帮助&#xff0c;就如同站在巨人的肩膀上的感觉吧 哈哈&#xff01;于是决定重新开始写博客&#xff0c;希望能帮助像我…

JavaFX TableView和ListView的点击事件

项目场景&#xff1a; 最近在用JavaFX做一个简易的商城界面&#xff0c;大概想实现这样的功能&#xff1a; 左边显示用户的最近五个购买的产品 使用ListView 点击ListView的项目会定位到相应的tablerow位置 方便用户快速查找中间显示所有可用产品 使用TableView 双击tablerow…

JavaFX【TableView使用详解】

目录 概述 组件 Student ObservableList TableView setCellValueFactory() TableColumn 1. Callback 2. PropertyValueFactory 增加到末行 1、tableView.getItems().add(Student s) 2、list.add(Student s) 删除指定行 1、tableView.getItems().remove(int i) 2、…

QT中TableView数据展示

QT中TableView数据展示 最近在学习QT,大量数据从数据库取出放入QT界面中展示&#xff0c;这时用到了tableView&#xff0c;一些简单的使用分享给大家。 创建数据模型 QStandardItemModel *modelnew QStandardItemModel(); QStandardItemModel是Qt库中的一个类&#xff0c;它…

JAVAFX的TableView基本用法

JAVAFX中的表格显示主要使用TableView 与TableView相关的类: TableColumn TableRow TableCell TablePosition TableViewFocusModel TableViewSelectionModel JavaFX TableView例子: import javafx.application.Application; import javafx.scene.Scene; import javafx.scene…

QT之Tableview

想要了解更多的tableview可以看这位博客Qt深入浅出&#xff08;十五&#xff09;QTableView​ 这里做了一个简单的学生系统查询功能显示Tableview&#xff1a; 表格视图控件QTableView&#xff0c;需要和QStandardItemModel, 配套使用&#xff0c;这套框架是基于MVC设计模式设…

QML TableView 使用详解

目录 一、—个简单的TableView实例 二、TableViewColumn 属性讲解 三、定制表格外观 3.1 itemDelegate3.2 rowDelegate3.3 headerDelegate3.4 定制表格外观后的示例 四、动态增删改查 TabelView TableView 就是 Qt Quick 为表格式呈现数据提供的组件。想必兄台用过 Excel…

QT控件之(TableView)的居中效果

T将tableView中的表头以及文本内容都进行居中处理 1、需要在构造函数中增加一句&#xff1a; //以下增加的是表头的效果 ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//布局排版是全部伸展开的效果2、就是直接对tableView的文本内…

QML学习十七:TableView的简单使用

若该文为原创文章&#xff0c;转载请注明原文出处 一、TableView TableView是Qt Quick为表格式呈现数据提供的组件。 TableView与ListView类似&#xff0c;相比之下多了滚动条、挑选、可调整尺寸的表头等特性&#xff0c;数据也是通过Model来提供&#xff0c;此篇使用的是内建…

JavaFX中TableView的使用

稍微说说JavaFX里面TableView怎么用&#xff0c;&#xff08;其实在JavaFX的源码中都有示例。。。&#xff09; 首先要了解TableView是用来做什么的&#xff0c;TableView是JavaFX的一个表视图&#xff0c;用来显示表格的&#xff0c;在TableView的类注释中写了 /*** see Tab…

Qt4实现TableView显示表格数据

最近又开始搞QT开发了&#xff0c;前面学的MVC啥的都忘得差不多了&#xff0c;重新整理一遍思路吧。 目前的需求是&#xff1a;读取文本文件&#xff0c;表格型数据&#xff0c;用tableview显示出来&#xff0c;最后画图。这涉及到三个问题&#xff0c;文件读写&#xff0c;数…