2016-02-08 32 views
0

我在我的storyboard中有UITableViewController,其中每个中间有三个单元格的标签在中间。 例如,如果用户通过选择一个项目点击第一个单元标签显示另一个带有项目列表的tableview,则返回到之前的tableView,并且应该在标签的位置打印项目名称。在代码中启动UITalbleViewController

#import <UIKit/UIKit.h> 
@interface carSelectCellTableViewCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel *carMake; 
@property (weak, nonatomic) IBOutlet UILabel *carModel; 
@property (weak, nonatomic) IBOutlet UILabel *carRego; 
@property (weak, nonatomic) IBOutlet UILabel *carYear; 

//the below label are the labels in the cells. 
@property (weak, nonatomic) IBOutlet UILabel *carSelected; 
@property (weak, nonatomic) IBOutlet UILabel *location; 
@property (weak, nonatomic) IBOutlet UILabel *service; 
@end 

#import "BookService.h" 
#import <Parse/Parse.h> 
#import "carSelectCellTableViewCell.h" 
@interface BookService() 
@end 

@implementation BookService 

@synthesize tableView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"cell tabbed"); 
    PFObject *temp = [customerCars objectAtIndex:indexPath.row]; 
    NSLog(@"%@", temp.objectId); 
    NSString *car = temp.objectId; 
     UIStoryboard *dashboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    UIViewController *change = [dashboard instantiateViewControllerWithIdentifier:@"bookAservice"]; 
    [self presentViewController:change animated:YES completion:nil]; 
    static NSString *Identifier = @"carSelectedCell"; 
    //here is where i'm calling the cell to change the label value when the selection is made. before dequeueReusableCellWithIdentifier there should be appropriate tableView Table. 
    carSelectCellTableViewCell *cell2 = [dequeueReusableCellWithIdentifier:Identifier]; 
    cell2.carSelected.text = @"selcted"; 

} 

如何以编程方式启动tableView。这样我可以将单元格标签值更改为所选项目。

回答

1

现在如果妳假设第一个表视图名称作为ParentViewController,第二个表视图childViewü可以做FOLL:

为此做出ParentController ChildController的代表。这允许ChildController将消息发送回ParentController,使我们能够发回数据。

要让ParentController成为ChildController的委托,它必须符合我们必须指定的ChildController协议。这告诉ParentController它必须实现哪些方法。

在ChildController.h中,#import下方,但上面的@interface指定了协议。

@class ChildController; 

@protocol ViewControllerBDelegate <NSObject> 
- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item; 
@end 

在ChildController.h下次还你需要设置一个委托财产ChildController.h

@property (nonatomic, weak) id <ChildControllerDelegate> delegate; 

在ChildController我们呼吁委托的消息时,我们弹出视图控制器。

对于这种情况,下面将didSelectRowAtIndex方法

NSString *itemToPassBack = @"Pass this value back to ParentController"; 
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack]; 

以上就是ChildController被调用。现在在ParentController.h中,告诉ParentViewController导入Child并符合其协议。

进口 “ChildController.h”

@interface ParentController:UIViewController中 在ParentController.m从我们的协议实现以下方法

- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item 
{ 
    NSLog(@"This was returned from ChildController %@",item); 
} 

我们需要做的最后一件事就是告诉ChildController是ParentController是我们将ChildController推入导航堆栈之前的委托。

ChildController *ChildController = [[ChildController alloc] initWithNib:@"ChildController" bundle:nil]; 
ChildController.delegate = self 
[[self navigationController] pushViewController:ChildController animated:YES];