如何 iPad 编程中使用自定义 TableViewCell 已翻译 100%

曾沙 投递于 2012/11/26 15:36 (共 5 段, 翻译完成于 01-07)
阅读 596
收藏 0
0
加载中
UITableViewCell类能够显示出各种各样的风格,但有时候我们需要适应不同的显示模式下的显示。今天的文章中,我们将使用table view去显示一系列自定义的cell。

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


点击Next,选择项目的存放路径,最后点击Create。
曾沙
翻译于 2012/11/26 15:38
1
这里需要添加两个文件,UITableViewController以及custom cell对应的xib文件。

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


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

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


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

接着打开 CellNib.xib 文件。在上面拖放几个 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。

曾沙
翻译于 2012/11/26 15:38
1

打开 TableViewController.h 然后添加这些属性:


#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController
@property (nonatomic, strong) NSArray *cellContent;
@property (nonatomic, strong) IBOutlet UITableViewCell *customCell;
@end
这个演示中,我们定义一个数组来记录所有cell的内容,还需要如下图那样,设置设置好 customCell的outlet。


现在打开TableViewController.m做出如下更改:
#import "TableViewController.h"
@interface TableViewController ()
@end
@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.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = 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 [[self.cellContent objectAtIndex:0] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 149.0f;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [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 = self.customCell;
        self.customCell = nil;
    }
    
    // Configure the cell…
    UILabel *textTarget;
    textTarget = (UILabel *)[cell viewWithTag:1]; //name
    textTarget.text = [[self.cellContent objectAtIndex:indexPath.row] objectAtIndex:0];
    textTarget = (UILabel *)[cell viewWithTag:2]; //addr1
    textTarget.text = [[self.cellContent objectAtIndex:indexPath.row] objectAtIndex:1];
    textTarget = (UILabel *)[cell viewWithTag:3]; //addr2
    textTarget.text = [[self.cellContent objectAtIndex:indexPath.row] objectAtIndex:2];
    textTarget = (UILabel *)[cell viewWithTag:4]; //phone
    textTarget.text = [[self.cellContent objectAtIndex:indexPath.row] objectAtIndex:3];
    textTarget = (UILabel *)[cell viewWithTag:5]; //cellPhone
    textTarget.text = [[self.cellContent objectAtIndex:indexPath.row] 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.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}
@end


曾沙
翻译于 2012/11/26 15:43
1

属性合成后,我们覆盖了 cellContent 的 getter 方法用于后实例化为保存姓名、地址和电话号码的数组。因为该表格视图只有一个段,因此 numberOfSectionsInTableView 返回 1. numberOfRowsInSection 方法返回 cellContent 数组中内部数组的个数。

而 heightForRowAtIndexPath 必须返回 CustomCell.xib 中单元格的高度。如果你一开始就进入此段代码,会发现数值有点不同。

为了改变单元格的背景色,我们必须定义一个委派方法 tableView:willDisplayCell:forRowAtIndexPath: 。这是我们唯一可用来修改背景颜色的地方。

tableView: cellForRowAtIndexPath: 方法首次试图获取一个可重用的单元格,如果没有找到(cell==nil) 的话,我们从 bundle 中加载 nib 文件,并设置到 customCell,然后设置 customCell 的值为 nil。之后我们使用他们的tag来配置单元格标签,每个标签的文本值存放于 cellContent 数组中。在这个方法的最后,我们返回单元格。

TableView: didSelectRowAtIndexPath: 在这里显示但没有实现。我们很可能会推一个新的视图以响应选择。

还剩下一件事:我们必须在 AppDelegate 中设置视图,这样才能在窗体中显示,打开 AppDelegate.h 并作如下改变:

#import <UIKit/UIKit.h>
#import "TableViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) TableViewController *tableViewController;
@property (strong, nonatomic) UINavigationController *navController;
@end
红薯
翻译于 2013/01/07 21:44
1
OK,现在运行程序,特别注意一下tableViewController的默认cell已经被我们的自定义 cell 替代。


用这种方式创建的 TableViewCell 能够包含任意控件而不仅仅只是Label与Image,也因为Files's Owner属于TableViewController,任何action方法必须先在viewController对应的类中事先定义好,当然这部分就是另一篇文章所谈到的事情了。 
曾沙
翻译于 2012/11/26 15:45
2
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(0)

返回顶部
顶部