2010-11-09 59 views
1

我有一个表视图,与IB创建添加headerview显示在表上方的一些文本(无法发布截图-http://i51.tinypic.com/2przbl5.jpg)。我想根据我的plist加载表格上方的文本,并使用objectforkey加载文本。但是,当我连接IB中的插座时,文字消失。这工作在一个标准的UIview,所以我不知道我在表视图中缺少什么。我是新来的编码,所以也许有更好的方法来做到这一点,或者我做错了什么?谢谢。iphone - UITableview - headerview不加载objectkey

.h file _________________________________________________ 


#import <UIKit/UIKit.h> 
@interface TourOverviewController : UITableViewController { 

NSArray *points; 
NSDictionary *tour; 
IBOutlet UITextField *nameTextField; 
IBOutlet UITextView *pointsTextView; 
} 

@property (nonatomic, retain) NSArray *points; 
@property (nonatomic, retain) NSDictionary *tour; 
@property (nonatomic, retain) UITextField *nameTextField; 
@property (nonatomic, retain) UITextView *pointsTextView; 


@end 

.m file______________________________________________________ 

    #import "TourOverviewController.h" 
#import "LoadingNames.h" 

@implementation TourOverviewController 
@synthesize points, tour, nameTextField, pointsTextView, 

- (void) viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

nameTextField.text = [tour objectForKey:NAME_KEY]; 
pointsTextView.text = [tour objectForKey:DIRECTIONS_KEY]; 
} 


- (void)viewDidLoad { 
NSArray *array = [[NSArray alloc] initWithObjects:@"Toy Story", 
         @"A Bug's Life", @"Toy Story 2", @"Monsters, Inc.", 
         @"Finding Nemo", @"The Incredibles", @"Cars", 
         @"Ratatouille", @"WALL-E", nil]; 
    self.points = array; 
    [array release]; 
    [super viewDidLoad]; 
} 
- (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 [points count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    NSString *rowString = [points objectAtIndex:row]; 
    cell.textLabel.text = rowString; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    [rowString release]; 

    return cell; 
} 

@end 

回答

0

林不知道我理解你的问题,但你可以创建2的Plist,点和旅游,然后用plist中的内容填写您的可变数组和字典。

编辑* * ** * ** * ** * ** * ** * ** * ** * ** * **

我错过了这个,但可能就像添加@“”eg

[tour objectForKey:@"NAME_KEY"]; 

,如果你想在其中更改数据不论什么原因你应该让你的数组和字典可变的。

这里是如此的代码来获取的plist并填写您的数组或字典(请务必将Plist档案设置为相应的类型,当你填充它们)

在您的viewDidLoad方法将这个

NSString *pointsList = [[NSBundle mainBundle] 
    pathForResource:@"points" ofType:@"plist"]; 
    points = [[NSMutableArray alloc]initWithContentsOfFile: pointsList]; 

NSString *tourList = [[NSBundle mainBundle] 
    pathForResource:@"tour" ofType:@"plist"]; 
    tour = [[NSMutableArray alloc]initWithContentsOfFile: tourList]; 

然后你有你的数组和字典充满了plist的内容。

希望这可以帮助,也许我误解了你想要做的。

+0

谢谢,我在我的rootcontoller有类似的东西。当我添加它时,它会运行并获得相同的结果。当我把相同的项目从tableheader中拿出来,并且在UIview中,所有的东西都连接好了......所以我想它与表格标题中的某些东西有关。 – alittlelost 2010-11-09 13:55:44

+0

这应该可以解决您的问题。 :) http://www.purplehazeapps.com/home/index.php?option=com_content&view=article&id=84:headerfooterinuitableview&catid=37:myblog&Itemid=171 – jarryd 2010-11-09 21:16:45

+0

感谢所有帮助你们!我只是想通了,当我从我的根控制器中推出页面时,我不小心删除了plist文件中的行。是的,它终于工作=) – alittlelost 2010-11-10 16:41:12

0

通常,当您使用UITableViewController时,您想要显示的文本必须放在表头中。 这里是你如何添加一个标签到表头:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,100)]; 
UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 50)]; 
[header setTextColor:[UIColor redColor]]; 
[header setText:@"Some Text"]; 
[headerView addSubview:header]; 

[self.tableView setTableHeaderView:headerView]; 
+0

我试着添加这个,它取代了我在IB中创建的headerview。我想在IB中使用标题,但我无法获取连接的插座加载。 – alittlelost 2010-11-09 13:58:06

+0

您是否已通过Interface Builder将代码中的IBOutlets连接到nib文件中的代码? – avicene 2010-11-10 05:54:40