iphone - UITableview - headerview不加载objectkeys

时间:2021-01-18 21:13:04

I have a table view, with an added headerview created in IB to display some text above the table (couldn't post the screenshot- http://i51.tinypic.com/2przbl5.jpg). I would like to load the text above the table based on my plist and I am using objectforkey to load the text. However when I connect the outlets in IB the text disappears. This worked in a standard UIview so I'm not sure what I'm missing in the table view. I'm new to coding so perhaps there a better way to do this or what am i doing wrong? thanks.

我有一个表视图,在IB中创建了一个添加的标题视图,以显示表格上方的一些文本(无法发布屏幕截图 - http://i51.tinypic.com/2przbl5.jpg)。我想基于我的plist加载表格上方的文本,我使用objectforkey加载文本。但是,当我连接IB中的插座时,文本消失。这在标准的UIview中工作,所以我不确定在表视图中我缺少什么。我是新手编码所以也许有更好的方法来做这个或我做错了什么?谢谢。

.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

2 个解决方案

#1


0  

Im not sure I understand your question but you could create 2 plists, points and tour and then fill your Mutable array and dictionary with the contents of the plist.

我不确定我理解你的问题,但你可以创建2个plist,points和tour,然后用plist的内容填充你的Mutable数组和字典。

EDIT****************************

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

I missed this before, but maybe it as simple as adding a @" " e.g.

我以前错过了这个,但也许就像添加@“”一样简单。

[tour objectForKey:@"NAME_KEY"];

You should make your array and dictionary mutable if you want to change the data within them for whatever reason.

如果您想要因任何原因更改其中的数据,您应该使您的数组和字典可变。

Here is so code to get the plist and fill your array or dictionary(be sure to set the plists to the appropriate type when you fill them)

以下是获取plist并填充数组或字典的代码(确保在填充时将plists设置为适当的类型)

Put this in your viewDidLoad method

把它放在你的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];

Then you have your array and dictionary filled with the contents of the plist.

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

Hope this helps, maybe I misunderstood want you want to do.

希望这有帮助,也许我误解了你想要做什么。

#2


0  

Usually when you are using a UITableViewController, text like the one you are trying to display has to go in the table header. Here is how you can add a label to a table header:

通常当您使用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];

#1


0  

Im not sure I understand your question but you could create 2 plists, points and tour and then fill your Mutable array and dictionary with the contents of the plist.

我不确定我理解你的问题,但你可以创建2个plist,points和tour,然后用plist的内容填充你的Mutable数组和字典。

EDIT****************************

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

I missed this before, but maybe it as simple as adding a @" " e.g.

我以前错过了这个,但也许就像添加@“”一样简单。

[tour objectForKey:@"NAME_KEY"];

You should make your array and dictionary mutable if you want to change the data within them for whatever reason.

如果您想要因任何原因更改其中的数据,您应该使您的数组和字典可变。

Here is so code to get the plist and fill your array or dictionary(be sure to set the plists to the appropriate type when you fill them)

以下是获取plist并填充数组或字典的代码(确保在填充时将plists设置为适当的类型)

Put this in your viewDidLoad method

把它放在你的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];

Then you have your array and dictionary filled with the contents of the plist.

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

Hope this helps, maybe I misunderstood want you want to do.

希望这有帮助,也许我误解了你想要做什么。

#2


0  

Usually when you are using a UITableViewController, text like the one you are trying to display has to go in the table header. Here is how you can add a label to a table header:

通常当您使用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];