欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解iOS App中UITableView的创建与内容刷新

程序员文章站 2023-12-11 22:28:28
uitableview几乎是ios开发中用处最广的一个控件,当然也是要记相当多东西的一个控件。 创建 首先创建一个新的项目,并添加一个mainviewcontrolle...

uitableview几乎是ios开发中用处最广的一个控件,当然也是要记相当多东西的一个控件。

创建
首先创建一个新的项目,并添加一个mainviewcontroller的class文件

详解iOS App中UITableView的创建与内容刷新

打开mainviewcontroller.h文件

@interface mainviewcontroller : uiviewcontroller<uitableviewdatasource,uitableviewdelegate> 
 
@property (nonatomic, retain) nsarray *datalist; 
@property (nonatomic, retain) uitableview *mytableview; 
 
@end 

tableview的数据源uitableviewdatasource。
tableview的委托uitableviewdelegate。
如果当前类是继承自uiviewcontroller,需要添加上面的代码,如果直接继承自uitableviewcontroller则不需要添加
然后打mainviewcontroller.m文件,初始化uitableview并显示在当前窗口

- (void)viewdidload 
{ 
 [super viewdidload]; 
 // 初始化tableview的数据 
 nsarray *list = [nsarray arraywithobjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil]; 
 self.datalist = list; 
  
 uitableview *tableview = [[[uitableview alloc] initwithframe:self.view.frame style:uitableviewstyleplain] autorelease]; 
 // 设置tableview的数据源 
 tableview.datasource = self; 
 // 设置tableview的委托 
 tableview.delegate = self; 
 // 设置tableview的背景图 
 tableview.backgroundview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"background.png"]]; 
 self.mytableview = tableview; 
 [self.view addsubview:mytableview]; 
} 

在初始化的时候,可以为tableview设置样式
第一种:列表 uitableviewstyleplain

详解iOS App中UITableView的创建与内容刷新

第二种:分组uitableviewstylegrouped

详解iOS App中UITableView的创建与内容刷新

创建并设置每行显示的内容

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath 
{ 
 static nsstring *cellwithidentifier = @"cell"; 
 uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellwithidentifier]; 
 if (cell == nil) { 
  cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue2 reuseidentifier:cellwithidentifier]; 
 } 
 nsuinteger row = [indexpath row]; 
 cell.textlabel.text = [self.datalist objectatindex:row]; 
 cell.imageview.image = [uiimage imagenamed:@"green.png"]; 
 cell.detailtextlabel.text = @"详细信息"; 
 return cell; 
} 

uitableviewcell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义uitableviewcell的样式
uitableviewcellstyledefault

详解iOS App中UITableView的创建与内容刷新

uitableviewcellstylesubtitle

详解iOS App中UITableView的创建与内容刷新

uitableviewcellstylevalue1

详解iOS App中UITableView的创建与内容刷新

uitableviewcellstylevalue2

详解iOS App中UITableView的创建与内容刷新

分组的tableview还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview 
{ 
 return 1; 
} 

设置内容缩进

- (nsinteger)tableview:(uitableview *)tableview indentationlevelforrowatindexpath:(nsindexpath *)indexpath 
{ 
 return [indexpath row]; 
} 

详解iOS App中UITableView的创建与内容刷新

设置cell的行高

- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath 
{ 
 return 70; 
} 

设置cell的隔行换色

- (void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath 
{ 
 if ([indexpath row] % 2 == 0) { 
  cell.backgroundcolor = [uicolor bluecolor]; 
 } else { 
  cell.backgroundcolor = [uicolor greencolor]; 
 } 
} 

详解iOS App中UITableView的创建与内容刷新

当选择指定的cell时,弹出uialertview显示选择的内容

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath 
{ 
  
 nsstring *msg = [[nsstring alloc] initwithformat:@"你选择的是:%@",[self.datalist objectatindex:[indexpath row]]]; 
 uialertview *alert = [[uialertview alloc] initwithtitle:@"提示" message:msg delegate:self cancelbuttontitle:@"确定" otherbuttontitles:nil, nil]; 
 [msg release]; 
 [alert show]; 
} 

详解iOS App中UITableView的创建与内容刷新

滑动选择的行后删除

- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath 
{ 
 nslog(@"执行删除操作"); 
} 

详解iOS App中UITableView的创建与内容刷新

uitableview的刷新:

[self.tableview reloaddata];

reloaddata是刷新整个uitableview,有时候,我们可能需要局部刷新。比如:只刷新一个cell、只刷新一个section等等。这个时候在调用reloaddata方法,虽然用户看不出来,但是有些浪费资源。

刷新局部cell:

复制代码 代码如下:

 nsindexpath *indexpath = [nsindexpath indexpathforrow:0 insection:0];
 [self.tableview reloadrowsatindexpaths:[nsarray arraywithobjects:indexpath,nil] withrowanimation:uitableviewrowanimationfade];

这样就可以很方便的刷新第一个section的第一个cell。虽然看起来代码多了,但是确实比较节省资源。尽量少的刷新,也是uitableview的一种优化。

局部刷新section:

nsindexset *indexset = [[nsindexset alloc] initwithindex:0];
[self.tableview reloadsections:indexset withrowanimation:uitableviewrowanimationfade];

上面这段代码是刷新第0个section。

刷新动画:

刷新uitableview还有几个动画:

typedef ns_enum(nsinteger, uitableviewrowanimation) {
 uitableviewrowanimationfade, //淡入淡出
 uitableviewrowanimationright, //从右滑入   // slide in from right (or out to right)
 uitableviewrowanimationleft, //从左滑入
 uitableviewrowanimationtop,  //从上滑入
 uitableviewrowanimationbottom, //从下滑入
 uitableviewrowanimationnone,   // available in ios 3.0
 uitableviewrowanimationmiddle,   // available in ios 3.2. attempts to keep cell centered in the space it will/did occupy
 uitableviewrowanimationautomatic = 100 // available in ios 5.0. chooses an appropriate animation style for you
};

上一篇:

下一篇: