`

[iOS开发教程-2]Customize that UIViewCell – Part 1: Using Interface Builder

    博客分类:
  • iOS
 
阅读更多

http://www.iphonedevcentral.com/customize-that-uiviewcell-part-1-using-interface-builder/

 

Customize that UIViewCell – Part 1: Using Interface Builder

If you followed my first tutorial on UITableView (linksource code ), you now have a very simple app that shows a list of DVD titles and clicking any of the titles shows you a detail view page with more information about the DVD.  That’s all nice but we really want to make it a little bit prettier. We could display the length of each movie right on the listing page. Also, we have this coverImage field in our data set, let’s use it.

What we want is for our home screen to look like this:

tutorial006

We can accomplish that by customizing UITableViewCell. There are 2 ways of going about it:

  1. Using Interface Builder
  2. Programmatically

In reality, you can also choose a hybrid approach where you create some UI elements in the Interface Builder and some programmatically.

Which option to choose?

The two approaches both bring some advantages and disadvantages with them.

Interface Builder

If you decide to go the Interface Builder route, you’ll find it very easy to create and customize your cells. Any subsequent edits can also be done quite easily since you’re simply rearranging elements visually. The downside is speed and performance since the system needs to render each view in a cell individually. If your table view has thousands of rows in it, this may/may not affect the performance of your app, depending how complicated your cell is.

Programmatically

This one involves a lot more work. You are responsible for creating each UI element by hand in the code. That can be very tedious and any edits you need to make in the future require code changes. Also, you’ll need to set up all the autosizing masks yourself. The upside is performance. Since the system will draw each cell as one view, the performance gain can be very significant.

Ok, let’s get to it…

1. Subclass UITableViewCell

Create a Cocoa Touch Class file and make it a subclass of UITableViewCell . Let’s name it DVDListingViewCell.m . We’ll use this class as a placeholder for our labels and the cover image. In DVDListingViewCell.h add the following code:

@interface DVDListingViewCell : UITableViewCell {
    IBOutlet UILabel *titleLabel;
    IBOutlet UIImageView *coverImageView;
    IBOutlet UILabel *featureLengthLabel;
}
 
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *featureLengthLabel;
@property (nonatomic, retain) UIImageView *coverImageView;
 
@end

We use the IBOutlet annotation here to later let Interface Builder know we want to connect these properties with the actual UI elements we’ll lay out.

We also need synthesize the properties we’ve just defined. This will create all setters and getters for us. Add this to DVDListingViewCell.m right below the @implementation directive.

@synthesize titleLabel, featureLengthLabel, coverImageView;

2. Design the cell in Interface Builder

In Xcode, double-click on any of the nib files (they should all be listed under NIB Files magic folder). Once in Interface Builder, choose New… from the File menu. You should be presented a dialog with several templates in it. From the Cocoa Touch category on the left, choose Empty .

tutorial007

You should now see an “Untitled” window with File’s Owner and First Responder in it. One thing missing right now is the actual cell view, let’s add it. From the Library window (Tools -> Library) locate Table View Cell . Drag it to the “Untitled” window. You should now see something like this:

tutorial008

Double clicking on the Table View Cell object will open up the actual cell view where we’re going to add our UI elements. Make the cell 120 pixels in height (you change the dimensions under the size tab in the properties window or by pressing Command  + 3 ). Drag a couple Lable s and an Image View to the cell to lay it out as shown below. Once done, save the file as DVDListingView . Make sure to save the file in your project directory and check the checkbox when prompted if you want to add the file into your project.

tutorial009

3. Prepare the RootViewController

Return back to Xcode and open up your RootViewController.h header file. We will add a cell instance variable of type DVDListingViewCell that we’ve created earlier. We will use this cell to actually draw on the screen. Modify your header file to make it look like this:

@interface RootViewController : UITableViewController {
    DvdLibraryDao *dao;
 
    IBOutlet DVDListingViewCell *_cell;
}

You may notice the IBOutlet annotation again. That is because we will be connecting this variable to the actual cell we created in Interface Builder.

Let’s switch to the implementation file of our RootViewController (RootViewController.m ). You may remember the method cellForRowAtIndexPath from the first tutorial . That’s the method that controls what cell is drawn at which row. We’ll want our cell to be a DVDListingViewCell so let’s modify the current version of it to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"LibraryListingCell";
 
    DVDListingViewCell *cell = (DVDListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"DVDListingView" owner:self options:nil];
        cell = [_cell autorelease];
        _cell = nil;
    }
 
    cell.titleLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
    cell.featureLengthLabel.text = [NSString stringWithFormat:@"%@ minutes",
                                    [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"featureLength"]];
    cell.coverImageView.image = [UIImage
                                 imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:@"coverImage"]];
 
    return cell;
}

There are few things going on so let’s analyze them line by line.

Line #5 was changed to initialize the cell local variable to the DVDListingViewCell instead of the generic UITableViewCell .

Lines #6-#10 contain the meat of the cell creation. It first loads the nib file we created. You can see we’re setting the owner to self . That is because the _cell instance variable will get initialized via the nib file, which I’m going to cover a little later.

And finally, Lines #12-#15 assign proper values to the labels and image view in our custom cell.

4. Connect everything in Interface Builder

Return back to Interface Builder and click on the File’s Owner icon. Open up the Identity Inspector (Tools -> Identity Inspector) and look at the Class drop-down menu. Locate RootViewController and select it. In the same window, you should now see the _cell instance variable that belongs to RootViewController . When the nib file is loaded, we’ll want the cell we designed in Interface Builder to be assigned to our _cell . Let’s do that next.

Open up Connections Inspector (Tools -> Connections Inspector) with File’s Owner icon still selected. You should see our _cell in the Outlets section. Next to it is a little hollow circle that turns into a plus (+) sign when you hover it. Click on the circle and drag over to the Listing View Cell window. When you drag over it, the cell should highlight with a label that says “Listing View Cell.” Release the mouse to complete the connection (the cell will blink).

tutorial010

Note: There is an alternate way of creating the connection; Click on the File Owner’s icon while pressing down the Control key and Ctrl-drag to the Listing View Cell icon. A little black pop-up window “Outlets” will appear. Select _cell and let go of the mouse.

tutorial011

Now let’s connect our two labels and the image view. Highlight the Listing View Icon and open up Identity Inspector (Command+4). Under the Class drop-down find and select our DVDListingViewCell . Once done, you should see all the outlets we defined earlier in the DVDListingViewCell class: coverImageView , featureLengthLabel and titleLabel . Open up Connections Inspector (Command+2) and check out the Outlets section. Just as before, drag a connection from the hollow circle next to titleLabel to the “My DVD” label, featureLengthLabel to the feature length label and finally coverImageView to the UIImageView object in our Listing View Cell .

tutorial012

Alternatively, you can Control-drag from the Listing View Cell icon to the Listing View Cell window and connect the three objects that way.

tutorial013 Save all your changes and return back to Xcode.

5. Set the cell height in RootViewController

After you’ve saved your changes in Interface Builder and return back to Xcode, go ahead and run the project. You’ll see that everything works well except one thing – the cells are all crammed together.

tutorial014

This is because we need to explicitly set the height of cells in each row when it differs from the default. We can correct that by implementing the heightForRowAtIndexPath delegate method in the RootViewController.m file.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120.0;
}

Rerun your project and you should now see the cells correctly sized and looking good.

tutorial006

Conclusion

Remember that this is just one of a couple ways to customize your cells. You can do all of this by hand, programmatically which is more work but may result in a better performance. We will cover that in the second part of this tutorial.

You can download the complete source code to this tutorial here: My DVD Library Xcode Project 02 .

 

 

 

分享到:
评论

相关推荐

    IOS5 Programming Cookbook

    Based on the provided information from "iOS 5 Programming Cookbook" by Vandad Nahavandipoor, we can derive a comprehensive set of knowledge points related to iOS development using Objective-C....

    【Linux设备管理】从devfs到udev:设备文件系统演变及其关键技术对比分析了Linux设备管理

    内容概要:本文详细介绍了Linux设备文件系统从devfs到udev的演变过程。devfs作为Linux 2.4时代的创新,通过引入内核空间的设备管理机制,简化了设备文件的创建和管理,但其存在稳定性问题和灵活性不足的缺点。udev则运行在用户空间,利用内核发送的热插拔事件和sysfs提供的信息,实现了设备文件的动态管理和高度自定义。它不仅解决了devfs的问题,还提供了更灵活、高效的设备管理方式,成为现代Linux系统中的主流选择。文章还探讨了两者在不同应用场景中的优劣,并展望了未来Linux设备管理的发展方向,强调了对新型设备的支持、虚拟化和容器环境的兼容性以及智能化的用户体验。 适合人群:对Linux系统有一定了解,特别是对设备管理感兴趣的开发人员、系统管理员和技术爱好者。 使用场景及目标:①理解Linux设备管理的历史和发展趋势;②掌握devfs和udev的工作原理及其在不同场景下的应用;③学习如何编写udev规则文件,实现设备的个性化管理和优化配置。 阅读建议:本文内容较为技术性,建议读者先了解基本的Linux设备管理概念。在阅读过程中,重点关注devfs和udev的区别和优势,并结合实际应用场景进行思考。对于udev规则文件的编写,可以通过实践和调试加深理解。

    三维路径规划中RRT与APF融合算法及其路径平滑处理

    内容概要:本文详细介绍了将快速随机树(RRT*)和人工势场(APF)相结合用于三维空间路径规划的方法。首先阐述了两种算法的工作原理,特别是APF如何通过引力和斥力向量引导RRT*的节点扩展,使得路径规划更加高效且能够有效避开障碍物。接着讨论了路径平滑处理的具体实现方式,如利用贝塞尔曲线进行路径优化,确保最终路径不仅平滑而且不会发生碰撞。此外,文中还提供了具体的代码片段来展示各个模块的功能,包括APF核心算法、RRT*扩展逻辑、碰撞检测以及路径平滑等。同时提到了一些潜在的改进方向,如引入速度场因素、采用不同的平滑算法等。 适合人群:对机器人路径规划、无人驾驶等领域感兴趣的开发者和技术爱好者。 使用场景及目标:适用于需要在复杂三维环境中进行高效路径规划的应用场合,如无人机飞行、自动驾驶汽车等。主要目的是提高路径规划的速度和质量,使生成的路径更加平滑、安全。 其他说明:本文不仅提供了理论解释,还有详细的代码实现,便于读者理解和实践。对于希望深入了解RRT*和APF融合算法并应用于实际项目的读者来说是非常有价值的参考资料。

    HikvisionIVMSGetShell-main.zip

    HikvisionIVMSGetShell-main.zip

    动态演示后缀表达式的计算方式

    动态演示后缀表达式的计算方式

    大学战队2021雷达站视觉算法源码.zip

    1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程设计、期末大作业和毕设项目的学生、或者相关技术学习者作为学习资料参考使用。 3、该资源包括全部源码,需要具备一定基础才能看懂并调试代码。 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip 大学战队2021雷达站视觉算法源码.zip大学战队2021雷达站视觉算法源码.zip

    MATLAB用户界面设计.pptx

    MATLAB用户界面设计.pptx

    程序员面试题精选100题.pdf

    程序员面试题精选100题.pdf

    牵牛花铅笔素材儿童教学课件模板.pptx

    牵牛花铅笔素材儿童教学课件模板

    基于C++的rviz机械臂各类仿真+源码+项目文档(毕业设计&课程设计&项目开发)

    基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档~ 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于C++的rviz机械臂各类仿真+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档

    三相电流型PWM整流Matlab仿真:电压外环与电流内环双闭环控制策略详解

    内容概要:本文详细介绍了三相电流型PWM整流的Matlab仿真过程,采用了电压外环和电流内环的双闭环控制策略。电压外环负责维持直流侧电压的稳定,通过PI调节器输出电流给定值;电流内环根据电压外环的给定值和实际检测到的三相电流,经过PI调节器产生PWM波来控制整流器的开关动作,实现对交流侧电流的精确控制。文中提供了详细的系统参数设置、PI调节器参数设定、主循环仿真代码以及坐标变换的具体实现方法。此外,还讨论了PWM调制、开关信号生成、仿真结果分析等内容,并附有说明文档和参考文献。 适合人群:从事电力电子、控制系统设计的研究人员和技术人员,尤其是对PWM整流器感兴趣的工程师。 使用场景及目标:适用于研究和开发三相电流型PWM整流器的人员,帮助他们理解和实现双闭环控制策略,提高系统的稳定性和效率。目标是在不同工况下实现直流侧电压的稳定和网侧电流的正弦化,达到单位功率因数运行。 其他说明:文中提供的代码和仿真模型已在MATLAB 2021b及以上版本测试通过,附带的说明文档包含了参数整定表、典型波形库和故障排查指南,有助于解决实际应用中的问题。

    五相永磁同步电机单相开路故障下的矢量容错控制技术解析

    内容概要:本文详细探讨了五相永磁同步电机(PMSM)在发生单相开路故障时的矢量容错控制方法。首先介绍了五相电机相较于三相电机的优势及其容错机制的基本原理。随后,文章深入讨论了故障发生时的电流重构策略,包括Clarke变换矩阵的修改、电流补偿系数的选择以及相位补偿的方法。此外,文中还涉及了矢量控制的具体实现,如矢量合成、SVPWM调制方式的调整、电流环整定的自适应算法等。最后,通过实测数据分析了不同故障条件下的系统表现,并提出了进一步的研究方向和技术改进措施。 适合人群:从事电机控制系统设计与开发的技术人员,尤其是关注五相永磁同步电机容错控制领域的研究人员。 使用场景及目标:适用于需要提高电机系统可靠性和容错能力的应用场合,如工业机器人、电动汽车等领域。主要目标是在单相开路故障情况下,确保电机能够继续稳定运行并尽可能减少性能损失。 其他说明:文章不仅提供了理论分析,还包括大量实际案例和代码片段,有助于读者更好地理解和应用相关技术。同时提醒读者,在进行容错控制设计时要考虑实际系统的非线性特性,避免因参数设置不当而导致系统不稳定。

    电力系统暂态分析中PSS对单机无穷大系统稳定性影响的Simulink仿真研究

    内容概要:本文详细探讨了在单机无穷大系统中加入电力系统稳定器(PSS)前后,系统在不同扰动条件下的暂态响应情况。首先介绍了同步发电机的基本参数配置及其连接方式,然后分别进行了无PSS和带有PSS两种情况下系统的稳态运行、小扰动以及三相短路故障仿真实验。结果显示,PSS能够显著提高系统的阻尼水平,有效抑制因各种原因引起的振荡现象,确保系统快速恢复稳定状态。 适用人群:从事电力系统分析、自动化控制领域的研究人员和技术人员。 使用场景及目标:适用于希望深入了解PSS在电力系统中作用机制的研究者,以及需要评估PSS对于特定应用场景下系统性能改善效果的专业人士。通过本案例的学习,可以帮助使用者掌握如何利用MATLAB/Simulink进行相关仿真实验的方法。 其他说明:文中提供了详细的建模步骤和参数设定指南,并附有多张对比图表帮助理解PSS的作用效果。同时提醒了一些常见的操作误区,如仿真步长的选择等。

    2023-04-06-项目笔记 - 第四百七十三阶段 - 4.4.2.471全局变量的作用域-471 -2025.04-19

    2023-04-06-项目笔记-第四百七十三阶段-课前小分享_小分享1.坚持提交gitee 小分享2.作业中提交代码 小分享3.写代码注意代码风格 4.3.1变量的使用 4.4变量的作用域与生命周期 4.4.1局部变量的作用域 4.4.2全局变量的作用域 4.4.2.1全局变量的作用域_1 4.4.2.471局变量的作用域_471- 2025-04-19

    病理分割-基于深度学习实现的腹部多器官分割算法-附项目源码-优质项目实战.zip

    病理分割_基于深度学习实现的腹部多器官分割算法_附项目源码_优质项目实战

    基于三菱PLC和触摸屏的停车场智能管理系统设计与实现

    内容概要:本文详细介绍了基于三菱PLC和三菱触摸屏构建的停车场智能管理系统。系统分为入口、出口和管理中心三大部分,分别负责车辆身份识别、车位检测、道闸控制、缴费结算等功能。三菱PLC作为核心控制器,通过梯形图编程实现了车辆检测、道闸控制等关键逻辑;三菱触摸屏提供人机交互界面,支持参数设置、状态监控等功能。文中还讨论了PLC与触摸屏之间的通信配置,以及如何通过物联网技术将系统接入云端。 适合人群:从事智能交通系统开发的技术人员,尤其是熟悉三菱PLC编程和触摸屏应用的工程师。 使用场景及目标:适用于新建或改造停车场项目,旨在提高停车场管理效率和服务质量,减少人工干预,实现智能化运营。 其他说明:文中提供了具体的硬件配置建议、PLC编程实例、触摸屏界面设计指南及通信协议解析,有助于读者快速理解和实施类似项目。

    Code_20250419.html

    Code_20250419.html

    基于MSP430F5529的无线温湿度传输系统设计与实现

    内容概要:本文详细介绍了基于MSP430F5529微控制器构建的无线温湿度传输系统的完整设计方案。系统集成了DHT11湿度传感器、DS18B20温度传感器、Nokia5110显示屏、按键模块、报警模块和nRF24L01无线传输模块。文章不仅涵盖了硬件连接细节,还深入讲解了各模块的驱动代码实现,包括传感器数据读取、数据显示、按键处理、报警机制和无线数据传输。此外,还讨论了一些优化措施,如低功耗模式的应用和传感器供电稳定性。 适合人群:具有一定嵌入式系统开发基础的技术人员,尤其是对MSP430系列微控制器感兴趣的开发者。 使用场景及目标:适用于需要实时监测环境温湿度并进行远程数据传输的场合,如智能家居、农业温室、工业环境监测等。目标是帮助读者掌握MSP430F5529的实际应用技能,理解各模块之间的协同工作原理。 其他说明:文中提供的代码片段可以直接应用于实际项目中,有助于快速搭建原型系统。同时,文章还提供了一些调试经验和常见问题解决方案,便于读者在实践中少走弯路。

    台达DVP PLC RS485通信实现多台变频器频率读写及启停控制

    内容概要:本文详细介绍了如何利用台达DVP PLC通过RS485接口与多台变频器进行通信,实现读取设定频率、设定变频器频率及控制启动和停止的功能。主要内容涵盖硬件连接、通讯参数设置、编程实例及常见问题解决方案。文中提供了具体的梯形图代码示例,解释了MODBUS RTU协议的应用,包括功能码的选择、寄存器地址的定义、CRC校验的处理方法等。 适合人群:从事工业自动化领域的工程师和技术人员,特别是那些需要掌握PLC与变频器通信技能的人。 使用场景及目标:适用于需要集成PLC控制系统并管理多个变频器的工作环境,如工厂生产线、自动化设备等。目标是提高系统的稳定性和可靠性,确保各个变频器能够按照预设的要求高效运作。 其他说明:文中强调了实际应用中的注意事项,如硬件连接的准确性、通讯参数的一致性、数据传输的安全性等,并给出了详细的调试建议和优化措施。此外,还提到了一些常见的陷阱及其应对策略,帮助读者更好地理解和解决问题。

Global site tag (gtag.js) - Google Analytics