Objective-C UI之UITableView详解

Objective-C UI之UITableView详解,第1张

UITableView在iOS开发中占据非常重要的位置,必须熟练掌握。

学习UITableView之前,先了解一下一些基本概念:

从上面可以了解到,section和row代表一个UITableViewCell在UITableView上的位置

下面,我们创建一个UITableView:

下面是UITableView的常用属性:

rowHeight 行高

separatorStyle 分隔线样式

separatorColor 分隔线颜色

tableHeaderView UITableView的置顶视图

tableFooterView UITableView置底视图

UITableView中有两个重要的属性:dataSource(遵循UITableViewDataSource协议)和delegate(遵循UITableViewDelegate协议)

其中dataSource是和显示数据相关的代理,delegate是和视图 *** 作相关的代理

UITableViewDataSource协议中有两个必须实现的协议方法:

1UITableView每个分区包含的行数:

UITableView的每一个单元格是UITableViewCell类的对象,默认提供了三个视图属性:

UITableView有一个重用池机制管理Cell,目的是使用尽可能少的Cell显示所有的数据

UITableView重用Cell的流程

在创建UITableView之后,需要注册一个Cell类,当重用池中没有Cell的时候,系统可以自动创建Cell。相关方法:

[tableView registerClass:(Class)cellClass forCellReuseIdentifier:(NSString )identifier];(可以使用不同identifier进行多次注册)

系统提供了一个获取重用池中Cell的方法(需要一个重用标识):

1UITableViewDataSource

2UITableViewDelegate

流程:

注意:编辑结束后,由于numberOfRowInSection这个协议方法只在tableView添加到父视图的时候调用一次,而且table上的数据都是由数组提供,因此,需要先改变数组中的数据,然后让table的协议重新调用进行重新赋值

即先修改数据源,在刷新table(使用[table reloadData]方法刷新)

1自定义Cell

一般而言,Cell在创建的时候的frame大小是(0,0,320,44),而我们设定的Cell的高度一般会大于44。因此:在自定义Cell中创建子视图的frame为CGRectZero。在Cell添加到tableView上的时候才给子视图设置frame,Cell添加到tableView的时候大小已经更改为tableView设定的大小,所以在自定义Cell的方法layoutSubviews中设置子视图的frame

2Model的使用

Model类的作用主要是为我们提供数据,一般我们的数据都是存放在数组和字典中,OC中的KVC就是帮助我们将字典转换为Model类而存在的

使用步骤:

注意:Model类要重写-(void)setValue:(id)value forUndefinedKey:(NSString )key,防止找不到和key值相同的属性时,会crash,当key值为系统关键字,可以在方法里面为对应的属性(属性名和系统关键字不冲突)赋值,比如_id = value;

3多种Cell混合使用

不同的Cell需要使用不同的重用标识符来进行区分,而重用标识符的区分需要根据不同的情况来区分,比如:

4自适应高度

UITableViewCell类能够显示出各种各样的风格,但有时候我们需要适应不同的显示模式下的显示。今天的文章中,我们将使用table view去显示一系列自定义的cell。

启动Xcode,选择"Create a new Xcode project",然后选择空应用程序模板,点击Next。命名为 CustomCells,然后照下图那样设置。

点击Next,选择项目的存放路径,最后点击Create。

这里需要添加两个文件,UITableViewController以及custom cell对应的xib文件。

Choose File | New > File ,然后添加一个名为 TableViewController 的UITableViewController。

如图:

对于这个controller,我们并不需要xib文件,所以直接点击Next创建。

重新创建文件,这次我们是创建一个空的 xib 文件,如下图:

点击Next,确保Device Family被设置为iPad,再点击Next,在默认路径下保存为 CellNib 文件。

接着打开 CellNibxib 文件。在上面拖放几个 label:

这里第一个Label的字体大小是27,字体是System Italic。而其他的Label全部都是默认设置。

下一步就是为文本依然是"Label"的Label设置tag。

将第一个大字体的Label设置tag=1,然后设置Address1,Address2,Phone,Cell右边的Label的tag分别为2,3,4,5。

接着需要修改xib的File's Owner的所属类。这里选择为 TableViewController。

打开 TableViewControllerh 然后添加这些属性:

#import <uikit uikith=""><span class="referer">@interface</span> TableViewController : UITableViewController

@property (nonatomic, strong) NSArray cellContent;

@property (nonatomic, strong) IBOutlet UITableViewCell customCell;<span class="referer">@end</span> </uikit>

这个演示中,我们定义一个数组来记录所有cell的内容,还需要如下图那样,设置设置好 customCell的outlet。

现在打开TableViewControllerm做出如下更改:

#import "TableViewControllerh"<span class="referer">@interface</span> TableViewController ()<span class="referer">@end</span>

@implementation TableViewController

@synthesize cellContent, customCell;

- (NSArray )cellContent

{

cellContent = [[NSArray alloc] initWithObjects:

[NSArray arrayWithObjects:@"Alex Ander",

@"213 4th St", @"Apt 17", @"555-555-5555", @"111-111-1111", nil],

[NSArray arrayWithObjects:@"Jane Doe",

@"4 Any Ave", @"Suite 2", @"123-456-7890", @"098-765-4321", nil],

[NSArray arrayWithObjects:@"Bill Smith",

@"63 Smith Dr", @"", @"678-765-1236", @"987-234-4987", nil],

[NSArray arrayWithObjects:@"Mike Taylor",

@"3145 Happy Ct", @"", @"654-321-9871", @"654-385-1594", nil],

[NSArray arrayWithObjects:@"Nancy Young",

@"98 W 98th St", @"Apt 3", @"951-753-9871", @"951-654-3557", nil],

nil];

return cellContent;

}

- (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations

// selfclearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller

// selfnavigationItemrightBarButtonItem = selfeditButtonItem;

}

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view

// eg selfmyOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return YES;

}

#pragma mark – Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView )tableView

{

// Return the number of sections

return 1;

}

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

{

// Return the number of rows in the section

return [[selfcellContent objectAtIndex:0] count];

}

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

{

return 1490f;

}

- (void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath )indexPath

{

cellbackgroundColor = [UIColor colorWithRed:1 green:1 blue:75 alpha:1];

}

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

{

static NSString CellIdentifier = @"Cell";

UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

[[NSBundle mainBundle] loadNibNamed:@"CellNib" owner:self options:nil];

cell = selfcustomCell;

selfcustomCell = nil;

}

// Configure the cell…

UILabel textTarget;

textTarget = (UILabel )[cell viewWithTag:1]; //name

textTargettext = [[selfcellContent objectAtIndex:indexPathrow] objectAtIndex:0];

textTarget = (UILabel )[cell viewWithTag:2]; //addr1

textTargettext = [[selfcellContent objectAtIndex:indexPathrow] objectAtIndex:1];

textTarget = (UILabel )[cell viewWithTag:3]; //addr2

textTargettext = [[selfcellContent objectAtIndex:indexPathrow] objectAtIndex:2];

textTarget = (UILabel )[cell viewWithTag:4]; //phone

textTargettext = [[selfcellContent objectAtIndex:indexPathrow] objectAtIndex:3];

textTarget = (UILabel )[cell viewWithTag:5]; //cellPhone

textTargettext = [[selfcellContent objectAtIndex:indexPathrow] objectAtIndex:4];

return cell;

}

#pragma mark – Table view delegate

- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath

{

// Navigation logic may go here Create and push another view controller

/

<#DetailViewController#> detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

// …

// Pass the selected object to the new view controller

[selfnavigationController pushViewController:detailViewController animated:YES];

/

}<span class="referer">@end</span>#import <uikit uikith="">

#import "TableViewControllerh"<span class="referer">@interface</span> AppDelegate : UIResponder <uiapplicationdelegate> @property (strong, nonatomic) UIWindow window;

@property (strong, nonatomic) TableViewController tableViewController;

@property (strong, nonatomic) UINavigationController navController;<span class="referer">@end</span> </uiapplicationdelegate></uikit>

- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions

{

selfwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch

selftableViewController = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];

selftableViewControllertitle = @"Table View";

selfnavController = [[UINavigationController alloc]

initWithRootViewController:selftableViewController];

[selfwindow addSubview:selfnavControllerview];

[selfwindow makeKeyAndVisible];

return YES;

OK,现在运行程序,特别注意一下tableViewController的默认cell已经被我们的自定义 cell 替代。

>

可以使用自定义的cell,然后往里面放label,为label赋tag值,通过tag值去获取label,然后获取label中的内容。

例:UILabel textLabel = [cell viewWithTag:10];

NSString text = textLabeltext;

NSIndexPath index = [NSIndexPath indexPathForRow:0 inSection:0];

[_myTableView cellForRowAtIndexPath:index];

获取的就是cell。

1大部分的都是叫你将 estimatedRowHeight = 0;来关闭自动布局适配来解决,可这样又会让我的cell高度没法自适应所以我们采用只更新cell内容不更新高度的方式来解决。

用例如下: 点击cell的点赞功能,使点赞数量加1并点亮点赞的图标。

主要步骤在 利用indexpath获取你所点击的哪一行cell,然后重新赋值即可,就不用再去刷新整个tab或者固定的一行celll 了,这样他就不会去计算高度也就不会发生跳动了

myindexrow 是点击的inddexpathrow

 NSIndexPathindexPath=[NSIndexPathindexPathForRow:myindexrow inSection:0];      mysquareCell1 mycell = [selfmytableview cellForRowAtIndexPath:indexPath]                mycellmylabel9text= [NSStringstringWithFormat:@"%ld",(long)(mymodup_like- mymoddown_like)];

mylabel9就是你想要刷新后改变的值。

重用机制 简单的说 意思 一行一行 的cell 都是在复用的, 滑动 tableview 的时候,刚离开视图的 cell 会被放到复用池 中,等下一个 cell需要 显示时,会先看复用池中有没有 cell 如果有的时候 ,就从复用池中拿出来cell ,没有的话就重新创建cell

大概就是这几句代码

static NSString cellName = @"cell";

UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:cellName];

if(cell == nil)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];

}

下面是使用。。。people 是另外就个类里的对象,就是一个 数组 里面 取东西, 很好理解的。。。

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

{

static NSString cellName = @"cell";

UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:cellName];

if(cell == nil)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];

}

celltextLabeltext = ((People )[[_dataArray objectAtIndex:indexPathsection] objectAtIndex:indexPathrow])peopleName;

celldetailTextLabeltext = [NSString stringWithFormat:@"%d",((People )[[_dataArray objectAtIndex:indexPathsection] objectAtIndex:indexPathrow])peopleAge];

return cell;

}

以上就是关于Objective-C UI之UITableView详解全部的内容,包括:Objective-C UI之UITableView详解、如何iOS 编程中使用自定义 TableViewCell、ios 得到点击cell里的内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9303625.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存