2011-11-03 84 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]; 

你不添加“新小区”像你描述的那样,使用相同的细胞,并对其进行填充与不同的数据。

相关问题