IOS学习之table view controller、table view cell

A table view controller, like many objects, has more than one init method. There is:

initWithCoder, for view controllers that are automatically loaded from a storyboard

initWithNibName, for view controllers that you manually want to load from a nib (a nib is like a storyboard but only contains a single view controller)

initWithStyle, for table view controllers that you manually want to create without using a storyboard or nib

There are four ways that you can make table view cells:

1. Using prototype cells. This is a probably the simplest and quickest way. You did this in ChecklistViewController.

2. Using static cells. You did this for the Add/Edit Item screen. This is limited to screens where you know in advance which cells you’ll have. The big advantage with static cells is that you don’t need to provide any of the data source methods (cellForRowAtIndexPath and so on).

3. By hand, what you did above. This is how you were supposed to do it before iOS 5. Chances are you’ll run across code examples that do it this way, especially from older articles and books. It’s a bit more work but also offers you most of the flexibility.

4. Using a nib file. A nib (also known as a XIB) is like a storyboard but it only contains a single view controller or in this case a single custom UITableViewCell object. This is very similar to using prototype cells, except that you can do it outside of a storyboard.

When you create a cell by hand you specify a certain cell style, which gives you a cell with a preconfigured layout that already has labels and an image view. For the All Lists View Controller you’re using the “Default” style but later in this tutorial you’ll switch it to “Subtitle”, which gives the cell a second, smaller label below the main label.

Using standard cell styles means you don’t have to design your own cell layout. For many apps these standard layouts are sufficient so that saves you some work. Prototype cells and static cells can also use these standard cell styles. The default style for a prototype or static cell is “Custom”, which requires you to use your own labels, but you can change that to one of the built-in styles with Interface Builder.

And finally, a warning: Sometimes I see code that creates a new cell for every row rather than trying to reuse cells. Don’t do that! Always ask the table view first whether it has a cell available that can be recycled using dequeueReusableCellWithIdentifier. Creating a new cell for each row will cause your app to slow down, as object creation is slower than simply re-using an existing object. Creating all these new objects also takes up more memory, which is a precious commodity on mobile devices. For the best performance, reuse those cells!

时间: 2024-12-29 18:39:34

IOS学习之table view controller、table view cell的相关文章

iOS开发报错之attempt to dismiss modal view controller whose view does not currently appear

刚才遇到一个问题,现在在这就当纪录一下,大家有遇到的能快速找到原因,分享一下啊. 在APP中,需要用户登录后才能使用,所以我通过更改APP的[UIApplicationsharedApplication].keyWindow.rootViewController来控制界面的跳转. 在使用过程中出现如下问题: 1.登录成功后点击注销按钮,弹出注销提示框UIAlertView: 2.注销成功后重新登录: 3.再次点击注销不再弹出UIAlertView. 提示如下警告: 点击注销按钮执行更改rootv

Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中(swift)

Don't Put Into Your View Controller别把View创建的代码放在VC中html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; } .CodeMirror-lines { padding: 4px 0px; } .CodeMirror pre { paddin

iOS 跳转出现attempt to dismiss modal view controller whose view does not currently ……的解决方案

今天在设置修改密码成功进行跳转到登录页面重新登录的时候,我写的原代码是: [self dismissViewControllerAnimated:YES completion:nil]; [UIApplication sharedApplication].keyWindow.rootViewController = [[loginViewController alloc]init]; 于是出现了在本页面跳到登录页面,然后又切回到本页面的状况.如图: 首先是修改密码的页面: 点击确认以后,跳转到登

【IOS笔记】View Controller Basics

View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen space for displaying content and therefore must be creative in how they present information to the user. Apps that have lots of information to display

iOS开发 - View Controller 的paging

在学习开源中国ios 源码时,发现其实现view controllers 的paging的方法如下: 第一.定义一个容器类的view controller,类似于UIPageViewController. 该vc的主要由两部分组成: a 一个title view,用来表明当前内容是什么,并且能够相应用户的点击,切换content b 一个table view controller (通过addChildViewController添加,并将table view controller 的view

M2在奋斗之ios开发--View Controller pragramming guide for IOS中文版

About View Controllers 视图控制器是应用程序数据和其视觉外形之间的一个至关重要的链接.无论何时,应用程序显示一个用户界面,其显示的内容都是由一个或一组互相合作的视图控制器管理.因此,视图控制器给你建立的应用程序提供了骨架. iOS提供了很多内置的视图控制器类来支持标准用户界面块(piece),比如导航和标签栏.作为开发应用程序的一部分,你还可以实现一个或多个自定义控制器来显示应用程序的特定内容. 概述 在模型-视图-控制器(MVC)设计模式里,视图控制器是传统的控制器对象,

View Controller容器

在 iOS 5 之前,view controller 容器是 Apple 的特权.实际上,在 view controller 编程指南中还有一段申明,指出你不应该使用它们.Apple 对 view controllers 的总的建议曾经是"一个 view controller 管理一个全屏幕的内容".这个建议后来被改为"一个 view controller 管理一个自包含的内容单元".为什么 Apple 不想让我们构建自己的 tab bar controllers

Custom Container View Controller

什么是Container View Controller?苹果文档是这么描述的:  A container view controller contains content owned by other view controllers. 也就是说一个View Controller显示的某部分内容属于另一个View Controller,那么这个View Controller就是一个Container,比如UIKit中的UINavigationController,UITabBarControl

Swift:超炫的View Controller切换动画

匿名社交应用Secret的开发者开发了一款叫做Ping的应用,用户可以他们感兴趣的话题的推送. Ping有一个很炫的东西,就是主界面和之间切换的动画做的非常的好.每次看到一个非常炫的动画,都不由得会想:“这个东西我要不要自己实现以下”.哈哈~~~ 这个教程里,你会学到如何用Swift实现这样的很酷的动画.你会学到如何使用shape layer,遮罩和使用UIViewControllerAnimnatedTransitioning协议和UIPercentDrivenInteractivetrans

iOS Programming Autorotation, Popover Controllers, and Modal View Controllers

iOS Programming Autorotation, Popover Controllers, and Modal View Controllers? 自动旋转,Popover 控制器,Modal view controller? 1.? In this chapter, you are going to make four changes to Homepwner's behavior that will tailor the app's behavior to whatever dev