2011-11-03 51 views
0

我想知道什麼是最好的方式去添加一個新的單元格到iOS的tableview。假設我有一個firstviewcontroller和一個secondviewcontroller。在第一個viewviewcontroller我有一個tableview已經填充了一些數據,我也有一個導航欄,頂部有一個添加按鈕。點擊添加按鈕將呈現一個新的視圖,然後我想將數據從secondviewcontroller傳遞給firstviewcontroller並使用它填充我的tableview。iOS:從另一個視圖添加新的單元格到桌面視圖

感謝, 山姆

回答

1

您secondViewController應該創建一個委託協議。你的firstViewController應該被指定爲它的委託。

一旦secondViewController保存它調用你的firstViewController應該實現

一個好的爲例此方法的數據: How do I create delegates in Objective-C?

爲了讓你開始:secondViewController.h

@protocol secondViewControllerDelegate; 

@interface secondViewController : UIViewController{ 
    id<secondViewControllerDelegate> __unsafe_unretained delegate; 
} 
@property (nonatomic, unsafe_unretained) id<secondViewControllerDelegate> delegate; 
@end 

@protocol secondViewControllerDelegate <NSObject> 
- (void)dataSaved; 
@end 
0

你可以用tableView數據源添加一個新類。與nesessary數據來啓動它後阿努數據換款的呼叫[的tableView的setDataSource:]:(從兩個視圖控制器使用委託合作它們。)

// DataSourceAtoZ.h 
#import <Foundation/Foundation.h> 
#import "MallsViewController.h" 

@interface DataSourceCategory : NSObject <UITableViewDataSource> { 
    NSMutableArray *data; 
    UITableView *tableView; 
} 
@property (nonatomic, retain) NSMutableArray *data; 

- (id)initWithData:(NSMutableArray *)d; 

@end 

然後,在你的代碼的任何地方,組成數據要在的tableView和設置數據源:

NSMutableArray *dt = [[NSMutableArray alloc] init]; 
       for (int i = 0; i < [categories count]; i++) { 
        NSMutableArray *names = [[NSMutableArray alloc] init]; 
        [names addObject:[[categories objectAtIndex:i] objectAtIndex:0]]; 
        for (int j = 1; j < [[categories objectAtIndex:i] count]; j++) { 
         NSRange match = [[[categories objectAtIndex:i] objectAtIndex:j] rangeOfString:[params objectAtIndex:0]]; 
         if (match.length != 0) { 
          [names addObject:[[categories objectAtIndex:i] objectAtIndex:j]]; 
         } 
        } 
        [dt addObject:names]; 
        [names release]; 
       } 
       dsCategory = [[DataSourceCategory alloc] init]; 
       dsCategory = [dsCategory initWithData:dt]; 
       [dt release]; 
0

你應該在App委託創建數組,然後打電話給他們,但是改變他們,你從那裏想..

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate]; 

一旦你有這條線,你可以在兩個類之間的連接,並呼籲委託例如任何方法:

[appDelegate populateMyArray]; 

你不添加「新小區」像你描述的那樣,使用相同的細胞,並對其進行填充與不同的數據。

相關問題