翻译于 2012/11/26 15:38
打开 TableViewController.h 然后添加这些属性:
#import <UIKit/UIKit.h> @interface TableViewController : UITableViewController @property (nonatomic, strong) NSArray *cellContent; @property (nonatomic, strong) IBOutlet UITableViewCell *customCell; @end
#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
属性合成后,我们覆盖了 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
评论删除后,数据将无法恢复
评论(0)