在代码中启动UITalbleViewController

时间:2016-02-08 01:41:14

标签: ios objective-c uitableview

我的UITableViewController中有storyboard,其中每个中间有3个带标签的单元格。 例如,如果用户单击第一个单元格标签,则显示另一个带有项目列表的tableview,通过选择一个项目,它将返回到上一个tableView,并且项目名称应打印在以下位置:标签。

#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 个答案:

答案 0 :(得分: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并遵守其协议。

import“ChildController.h”

@interface ParentController:UIViewController 在ParentController.m中,从我们的协议

中实现以下方法
- (void)addItemViewController:(ChildController *)controller didFinishEnteringItem:(NSString *)item
{
    NSLog(@"This was returned from ChildController %@",item);
}

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

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