2012-06-04 53 views
0

我有一個UIViewController保存按鈕。當我點擊它時,我將一個字符串添加到UITableViewController中的一個數組中。我有一個segue然後進入表視圖,我希望數組中的新對象顯示爲表中的一行。將行添加到另一個UIView的TableView中

我怎樣才能從視圖控制器的數據顯示在表視圖控制器?
我在做什麼是行不通的。

的UIViewController:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if([[segue identifier] isEqualToString:@"showFindTable"]) 
    { 
     FindTableViewController *findTableVC = [segue destinationViewController]; 
     findTableVC.titles addObject:titleField.text]; 
     [findTableVC.tableView reloadData]; 
    } 
} 

的UITableViewController:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.titles = [[NSMutableArray alloc] init]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"FindCell"; 
    FindTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.title.text = [self.titles objectAtIndex:[indexPath row]]; 

    return cell; 
} 

裏面的表視圖控制器I初始化陣列。所以當我添加對象時,它只會在調用viewDidLoad時恢復爲空數組?我計劃最終堅持使用核心數據的數據。所以我認爲如果這是問題就可以解決問題。在那之前,我怎麼才能使它工作?

我試圖用一個字符串初始化標題數組。該字符串顯示。所以它看起來像是因爲我每次加載表視圖都重新初始化數組。我應該在哪裏/我應該在其他地方初始化數組?

+0

你實現數據源方法: - (NSInteger)ta bleView:(UITableView *)tableView numberOfRowsInSection :(NSInteger)節? – Mike

+0

是的,我只是返回1. – Ryan

回答

0

在我的項目,我用這樣的

克里特對象要數據添加到單元格如下類。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"WhatsNewCell"; 

WhatsNewCell *cell = (WhatsNewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
//if (nil == cell) 
{ 
    cell = [[WhatsNewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 
return cell; 
} 

但這裏WhatsNewCell是的UITableViewCell 的子類,並在類中創建方法和添加像

- (void)createCellLabels 
{  

albumNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 5.0, 200.0, 50.0)]; 
albumNameLabel.textAlignment = UITextAlignmentLeft; 
albumNameLabel.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1]; 
albumNameLabel.font = [UIFont boldSystemFontOfSize:15.0]; 
albumNameLabel.backgroundColor = [UIColor clearColor]; 
[self.contentView addSubview:albumNameLabel]; 

artistNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 28.0, 200.0, 50.0)]; 
artistNameLabel.textAlignment = UITextAlignmentLeft; 
artistNameLabel.textColor = [UIColor grayColor]; 
artistNameLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
artistNameLabel.backgroundColor = [UIColor clearColor]; 
[self.contentView addSubview:artistNameLabel]; 

genresLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 200.0, 25.0)]; 
genresLabel1.textAlignment = UITextAlignmentLeft; 
genresLabel1.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1]; 
genresLabel1.font = [UIFont boldSystemFontOfSize:15.0]; 
genresLabel1.backgroundColor = [UIColor clearColor]; 
[self.contentView addSubview:genresLabel1]; 

} 

可能是其對您有所幫助。

+0

感謝您的信息。不過,我認爲這並不符合我的情況。我想動態添加到數組中,然後將顯示在我的表中。但我的錶行數總是0,所以我不知道發生了什麼。 – Ryan

0

您可以使用委託:

ViewController.h(聲明代表)

@protocol delegateName <NSObject> 
- (void)prepareForSegue:(NSString)strToAdd; 
@end 

ViewController.m

- (void)save:(id)sender { 
    [delegete prepareForSegue:titleField.text]; 
} 

的UITableViewController

- (void)prepareForSegue:(NSString)strToAdd { 
    [self.title addObject:strToAdd]; 
    [self.tableView reloadData]; 
} 
相關問題