原創(chuàng)|其它|編輯:郝浩|2012-08-26 22:46:14.000|閱讀 613 次
概述:UITableViewCell類能夠按照已有的不同標(biāo)準(zhǔn)配置顯示出相應(yīng)的風(fēng)格內(nèi)容,但有時候我們需要將內(nèi)容顯示為非標(biāo)準(zhǔn)的自定義模式,如何才能在iPad編程里面實現(xiàn)這一顯示功能呢?下面就由慧都小編來為大家講解一下具體過程:
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
UITableViewCell類能夠按照已有的不同標(biāo)準(zhǔn)配置顯示出相應(yīng)的風(fēng)格內(nèi)容,但有時候我們需要將內(nèi)容顯示為非標(biāo)準(zhǔn)的自定義模式,如何才能在iPad編程里面實現(xiàn)這一顯示功能呢?下面就由慧都小編來為大家講解一下具體過程:
啟動Xcode,選擇"Create a new Xcode project",然后選擇空應(yīng)用程序模板,點擊Next。命名為 CustomCells,然后照下圖那樣設(shè)置。
點擊Next,選擇項目的存放路徑,最后點擊Create。
這里需要添加兩個文件,UITableViewController以及custom cell對應(yīng)的xib文件。
Choose File | New > File ,然后添加一個名為 TableViewController 的UITableViewController。
如圖:
對于這個controller,我們并不需要xib文件,所以直接點擊Next創(chuàng)建。
重新創(chuàng)建文件,這次我們是創(chuàng)建一個空的 xib 文件,如下圖:
點擊Next,確保Device Family被設(shè)置為iPad,再點擊Next,在默認(rèn)路徑下保存為 CellNib 文件。
接著打開 CellNib.xib 文件。在上面拖放幾個 label:
這里第一個Label的字體大小是27,字體是System Italic。而其他的Label全部都是默認(rèn)設(shè)置。
下一步就是為文本依然是"Label"的Label設(shè)置tag。
將第一個大字體的Label設(shè)置tag=1,然后設(shè)置Address1,Address2,Phone,Cell右邊的Label的tag分別為2,3,4,5。
接著需要修改xib的File's Owner的所屬類。這里選擇為 TableViewController。
打開 TableViewController.h 然后添加這些屬性:
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController
@property (nonatomic, strong) NSArray *cellContent;
@property (nonatomic, strong) IBOutlet UITableViewCell *customCell;
@end
這個演示中,我們定義一個數(shù)組來記錄所有cell的內(nèi)容,還需要如下圖那樣,設(shè)置好 customCell的outlets。
現(xiàn)在打開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
#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
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.tableViewController = [[TableViewController alloc] initWithStyle:UITableViewStylePlain];
self.tableViewController.title = @"Table View";
self.navController = [[UINavigationController alloc]
initWithRootViewController:self.tableViewController];
[self.window addSubview:self.navController.view];
[self.window makeKeyAndVisible];
return YES;
特別注意一下tableViewController的默認(rèn)cell已經(jīng)被我們的自定義 cell 替代,用這種方式創(chuàng)建的 TableViewCell 能夠包含任意控件而不僅僅只是Label與Image。運行結(jié)果如下:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:huidu