gpt4 book ai didi

objective-c - UITableView 不会显示 headerView.xib

转载 作者:太空狗 更新时间:2023-10-30 03:51:21 25 4
gpt4 key购买 nike

我正在尝试让 UITableView 显示我创建的 headerView.xib,但在构建它之后,没有任何显示。想把UITableView变成可编辑的,在ItemsViewController.m文件中添加了三个方法,但是没有显示。

怎么了?提前致谢。

相关文件如下:

ItemsViewController.h

#import <Foundation/Foundation.h>

@interface ItemsViewController : UITableViewController
{
IBOutlet UIView *headerView;
}

-(UIView *)headerView;
-(IBAction)addNewItem:(id)sender;
-(IBAction)toggleEditingMode:(id)sender;

@end

ItemsViewController.m

#import "ItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@implementation ItemsViewController // Incomplete implementation

-(id)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
for(int i = 0; i < 5; i++) {
[[BNRItemStore sharedStore]createItem];
}

}
return self;
}

-(id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore sharedStore]allItems]count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


// Check for a reusable cell first, use that if it exists
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

// If there is no reusable cell of this type, create a new one
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}

// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
BNRItem *p = [[[BNRItemStore sharedStore]allItems]objectAtIndex:[indexPath row]];

[[cell textLabel]setText:[p description]];

return cell;
}

-(UIView *)headerView
{
// If we haven't loaded the headerView yet
if (!headerView) {
//Load HeaderView.xib
[[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil];
}
return headerView;
}

-(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec
{
return [self headerView];
}

-(CGFloat)tableView:(UITableView *)tv heightForHeaderInSection:(NSInteger)sec
{
// The height of the header view should be determined from the height of the
// view in the XIB file
return [[self headerView]bounds].size.height;
}



@end

最佳答案

以下一项或两项设置不正确。首先,HeaderView.xib 中的 File's Owner 对象需要将其类设置为 ItemsViewController。之后,headerView outlet 需要从 File's Owner 连接到 xib 中的顶级 View 。

关于objective-c - UITableView 不会显示 headerView.xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com