2010-11-09 56 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