我需要自定義UITableViewController
的標題部分,其中爲每個部分返回不同的標題文本(也從數據源獲取數據)。這是通過以下方式完成的:爲UITableViewController自定義標題部分
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *temp = [listOfMBeans allKeys];
DLog(@"MBean details: %@", temp);
NSString *title = [temp objectAtIndex:section];
DLog(@"Header Title: %@", title);
return title;
};
這很好,我可以看到預期的輸出。不過,我也需要改變文本的字號,並尋找類似的問題後,我已經實現了以下內容:
- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
DLog(@"Custom Header Section Title being set");
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
[headerView addSubview:label];
return headerView;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0;
}
但是似乎代碼永遠不會調用。我的理解是,UITableViewController
默認情況下將其設置爲委託,但似乎我錯了。
的UITableViewController
以這種方式創建(如分層數據的一部分):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProjectDetails *detailViewController = [[ProjectDetails alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.project = [listOfMetrics objectAtIndex:indexPath.row];
// Push the detail view controller.
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
}
什麼樣的變化,我應該做這方面的工作? 謝謝。
它不是真的我清楚,那你有什麼要求? – rptwsthi 2011-05-31 13:51:59
確保你設置了新的表格視圖委託,也許在'projectDetails'的init方法中。 – theiOSDude 2011-05-31 13:58:01
@rptwsthi基本上如何使我的UITableViewController ProjectDetails調用viewForHeaderInSection以自定義標題視圖 – 2011-05-31 14:39:43