保存。
三、代碼中動態加載Table View Cell XIB:
打開oneViewController.h讓oneViewController實現協議UITableViewDataSource,UITableViewDelegate,即:
oneViewController.m中實現下面的接口方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return20;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with
dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *CustomCellIdentifier =@"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell ==nil) {
NSArray *nib = [[NSBundlemainBundle]loadNibNamed:@"Cell" owner:selfoptions:nil];
// if ([nib count] > 0) {
// cell = self.tableViewCell;
// } else {
// NSLog(@"failed to load CustomCell nib file!");
// }
cell = [nib objectAtIndex:0]; // 注釋掉的和該句是兩種方式,在這裡兩種方式都行。但是如果沒有上面紅色處(圖XXX)的拖拽連接過程,這裡只能使用nib objectAtIndex方式。
}
NSUInteger row = [indexPathrow];
NSLog(@"++++++++++++++ jonesduan %s, cell row:%d", __func__, row);
UILabel *cellLabel = (UILabel *)[cellviewWithTag:1];
cellLabel.text = [NSStringstringWithFormat:@"cell index: %d", row];
UIButton *cellButton = (UIButton *)[cellviewWithTag:2];
[cellButton setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[cellButton setTitleColor:[UIColororangeColor]forState:UIControlStateHighlighted];
[cellButton setTitle:@"Press me!"forState:UIControlStateNormal];
[cellButton setTitle:@"Pressed!"forState:UIControlStateHighlighted];
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Modified by jonesduan.
return80;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.labelsetBackgroundColor:[UIColorgrayColor]];
[self.buttonsetTitleColor:[UIColorgreenColor]forState:UIControlStateNormal];
[self.buttonsetTitleColor:[UIColororangeColor]forState:UIControlStateHighlighted];//這裡控制的是oneViewController.xib中的label和button,前面已經拖拽連接過。
}
這樣完成後。CMD + R運行一下看看,發現界面如下圖所示:
並沒有顯示Cell.xib中的Cell Label和Cell Button,下面是最重要的一點,也是一個人研究了很久才知道的,即:選中oneViewController.xib中view下的子Table View,切換到connections inspector,將Table View的dataSource和delegate均與File's owner相拖拽進行連接。如下圖:
OK,成功!
總結:通過xib自定義view比較重要的一點就是這個view被哪個view controller使用,比如xxxViewController,就要在identity inspector中Custom Class指定對應的加載類為xxxViewController,然後選中File's owner(中間大窗口中的Placeholders下),並切換到connections inspector將Outlets下面的對象與中間大窗口中的Objects(與Placeholders同級別,在Placeholders下面)下的根視圖相連接。最後別忘了Table View的dataSource和delegate均與File's owner相拖拽進行連接。
APPLE開發最不習慣的就是拖拽連接,在此記錄一下作為個人參考,免得以後忘記了。
demo下載地址:http://download.csdn.net/detail/duanyipeng/4063778
摘自 Code Heaven