2014-09-26 30 views
-1

我有兩個viewcontrollers都有tableviews。第一個視圖控制器在每一行中都有一個標題,而另一個空行。我想要的是能夠複製第一個視圖控制器中選定行的內容並將它們粘貼到第二個視圖控制器中的空行中。如何將選定的行從一個UITableView複製到另一個?

這裏是.m文件:

#import "SimpleTableViewController.h" 

@interface SimpleTableViewController() 

@end 


@implementation SimpleTableViewController 
{ 
    NSArray *list; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    list = [NSArray arrayWithObjects:@"Pic 1", @"Pic 2", @"Pic 3", @"Pic 4", @"Pic 5", nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [list count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *simpleTableIndentifier= @"simpleTableCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIndentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIndentifier]; 

      } 
    cell.textLabel.text = [list objectAtIndex:indexPath.row]; 
    cell.imageView.image = [UIImage imageNamed:@"image.jpg"]; 
    return cell; 

} 

@end 

我是新來的UITableView所以我做這個學習的目的。

謝謝

+0

在兩個數據源之間複製數據並重新加載。 – 2014-09-26 13:37:59

回答

1

我會做什麼,是創建一個新的NSMutableArray,是爲了存儲選定單元格的值,然後將該數組傳遞到新表作爲它的數據源。

像這樣:

@implementation SimpleTableViewController 
{ 
    NSArray *list; 
    NSMutableNArray *selectedCells; // store the selected cells here 

} 

- (void)viewDidLoad 
{ 

    // .... your code here 

    // initialize your selected cells array 
    selectedCells = [[NSMutableArray alloc] init]; 
} 

現在使用通過UITableViewDelegatedidSelectRowAtIndexPath方法,它是提供給你獲得所選行的數據:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *cellText = cell.textLabel.text; 
    UIImage *imageView = cell.imageView.image; 
    // Now add whatever you want to your selectedCells array now 
    // that you have a reference to the selected cell. 
} 

通過簡單的設置是這樣,你可以現在將您的selectedCells數組傳遞給您的新的tableView作爲數據源。

希望這會有所幫助。我沒有測試過代碼,所以只是爲了指出你的方向。

1

ü可以得到選中行如下: -

//First TableViewController 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    secondTableViewObj = [[SecondTableView alloc]initWithNibName:@"SecondTableView" bundle:nil]; 
    secondTableViewObj.dict = [NSDictionary dictionaryWithObjectsAndKeys:@"test",@"title",@"test2",@"detail", nil]; 

    secondTableViewObj.pass_img = [UIImage imageNamed:@"Icon"]; 

    //Alternative of this would be to use segue and do all passing of object's in prepareForSegue method but don't forget to put identifier. 
    [self.navigationController pushViewController:next animated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 

//Second TableViewController 
In SecondTableView.h file 

#import <UIKit/UIKit.h> 

@interface NewViewController : UIViewController 

@property (assign) UIImage *pass_img; 
@property (nonatomic, retain) NSDictionary *dict; 
@end 

In SecondTableViewCon.m file 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identtifier [email protected]"yourCellIndentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identtifier]; 

    if(cell==nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identtifier]; 
    } 

    //You need to provide tag value to your component in your xib/storyboard and set them with value's as below. 
    UILabel *cellTitleLabel = (UILabel *)[cell viewWithTag:0]; 
    UILabel *cellDetailLabel = (UILabel *)[cell viewWithTag:1]; 
    UIImage *cellImg = (UIImage *)[cell viewWithTag:2]; 

    cellTitleLabel.text = [dict objectForKey:@"title"]; 
    cellDetailLabel.text = [dict objectForKey:@"detail"]; 
    cellImg = img; 

    return cell; 
    } 

別的然後不要讓我知道。

+0

你能給我一個小例子,說明如何將我的單元格的內容傳遞給第二個viewTable?我在這裏有點困惑。謝謝 – user2828182 2014-09-28 07:09:06

0

首先,這裏是你可能想要的答案:

使用indexPathsForSelectedRows。在您從複製的視圖控制器中,您必須實現cellForRowAtIndexPath,因此indexPathsForSelectedRows可以以某種方式成爲「反向」的一部分。如果您知道索引路徑與您的數據集的某個成員是1:1(並且很可能),那麼調用indexPathsForSelectedRows可以通過獲取索引路徑並找到關聯的數據集成員來獲取所需的數據。現在

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
... 
cell.textLabel.text = ((MyClass *)self.myDataSet[indexPath.row]).someTextProperty; 
... 
} 

- (void)copyRowsToTableViewB { 
    for (NSIndexPath path in self.indexPathsForSelectedRows) { 
     // Add to table B's datasource and re render 
     [tableBDataSource.someMutableArrayMaybe addObject:self.myDataSet[indexPath.row] 
    } 
} 

,這將是更好,更容易複製數據,而不是一個視圖的內容。例如,創建一個SimpleTableViewDataSourceAndDelegate,實現代理&給定適當的服務/ ORM /任何依賴關係的數據源協議。

@interface SimpleTableViewDataSourceAndDelegate : NSObject<UITableViewDelegate, UITableViewDataSource> 

... 

@interface UIViewControllerA : UIViewController<UITableViewDelegate, UITableViewDataSource> 
- (id) initWithBaseDelegate:(NSOjbect<UITableViewDelegate> *) baseDelegate dataSource:(NSObject<UITableViewDataSource> *)baseDataSource 

這將允許他們在填充數據源時共享相同的被引用對象。

相關問題