2013-05-01 104 views
-1

我是一個開始iPhone開發人員。NSMutableArray addObject不工作uitableview

我有一個UITableView但我無法使其與NSMutableArray一起使用。

我已經分配並初始化了我的數組:dataArray我也連接了我的tableview的dataSourcedelegate

這是我的代碼:

SecondViewControllerModal.m

@implementation SecondViewControllerModal 

-(IBAction)addArray:(id)sender 
{ 
    NSArray *content = [[NSArray alloc] initWithObjects:subjectName.text, setLetter.text, setOrder.text , nil]; 
    SecondViewController *c=[[SecondViewController alloc] init]; 
    [c addToArray:content]; 
} 

... 

addArray被稱爲當用戶按下一個按鈕)

SecondViewController.m

-(void)addToArray:(NSArray *)content { 

    NSString *setSubject = [content objectAtIndex:0]; 
    NSString *setLetter = [content objectAtIndex:1]; 
    NSString *setOrder = [content objectAtIndex:2]; 

    [dataArray addObject:setSubject]; 

    [tableView reloadData]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [dataArray count]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self->tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; 


    return cell; 
} 

注意:我正在使用故事板和一個選項卡式視圖應用程序,我只需要在iPad上運行它,如果這有什麼區別。

如果我沒有包含足夠的細節或者您對代碼有任何疑問,請發表評論。

編輯:這是我酥料餅看起來像在Xcode:

+0

你用的NSLog語句檢查?內容是否有效?是數據陣列? SETSUBJECT? – Mundi 2013-05-01 19:50:40

+0

@Mundi是啊我做過 – 2013-05-01 20:04:31

+0

你究竟在屏幕上展示第二個視圖控制器視圖?如果你正在使用故事板,你可能不應該只分配/ init第二個視圖控制器,但你應該觸發一個segue ... – Wain 2013-05-01 20:15:51

回答

1

您的每一次按下「圓矩形」按鈕,你要創建一個新的second view controller,傳遞給它的一些數據,要求它做一些事,然後把它扔掉。

你需要更新你的addArray:方法來調用addToArray:現有second view controller

您應該通過將second view controller的引用傳遞給模式,以便在按下條形按鈕時顯示它。

在SecondViewControllerModal.h:

@property (weak, nonatomic) SecondViewController *owningSecondViewController; 

在SecondViewController.m:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"..."]) { 
     SecondViewControllerModal *modalViewController = (SecondViewControllerModal *)segue.destinationViewController; 
     modalViewController.owningSecondViewController = self; 
    } 
} 

最後:

- (IBAction)addArray:(id)sender 
{ 
    NSArray *content = [[NSArray alloc] initWithObjects:subjectName.text, setLetter.text, setOrder.text , nil]; 

    [self.owningSecondViewController addToArray:content]; 
} 
+0

ahhh thaat有意義,但我將如何通過當前視圖?對不起objc noob here – 2013-05-01 20:42:28

+0

我的意思是我究竟寫什麼? – 2013-05-01 20:44:23

+0

我不以編程方式添加模式視圖控制器,我使它在界面生成器 – 2013-05-01 21:14:36

相關問題