从tableview切换回根视图时,我的代表无法工作

时间:2016-08-13 05:00:58

标签: ios objective-c uitableview

这是我的citylistTableView控制器,当我按下一个单元格并将所选单元格的文本发送回视图控制器的第一个单元格时,我正在尝试返回viewcontrollor

#import <UIKit/UIKit.h>
//#import "ViewController.h"
@class CityListTableViewController;

@protocol CityListTableViewControllerDelegate <NSObject>
-(void)done:(CityListTableViewController*)controllor selectedCity:(UITableViewCell*)citySelected;
@end

@interface CityListTableViewController : UITableViewController{
    NSMutableArray* cities;
}
@property (nonatomic,weak) id<CityListTableViewControllerDelegate> cityDelegate;

@end

#import "CityListTableViewController.h"

@interface CityListTableViewController ()

@end

@implementation CityListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    cities = [[NSMutableArray alloc] init];
    [self initCities];
}

-(void)initCities{
    [cities addObject:@"洛杉矶"];
    [cities addObject:@"旧金山"];
    [cities addObject:@"芝加哥"];
    [cities addObject:@"西雅图"];
    [cities addObject:@"华盛顿"];
    [cities addObject:@"纽约"];
    [cities addObject:@"迈阿密"];
    [cities addObject:@"圣地亚哥"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return [cities count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cities" forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = [cities objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(cell.textLabel.text);
    [self.cityDelegate done:self selectedCity:cell];

    //go back to root view controllor
    [self.navigationController popToRootViewControllerAnimated:YES];


}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

下一步:

#import <UIKit/UIKit.h>
#import "CityListTableViewController.h"

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITabBarDelegate,CityListTableViewControllerDelegate>{

    IBOutlet UITableView *setTourTableView;
    NSMutableArray *tourSetting;



}

@end

最后:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Generate a black tab bar

    // Set the selected icons and text tint color

    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.93 blue:0.93 alpha:1.0];
    setTourTableView.backgroundColor = [UIColor colorWithRed:0.94 green:0.93 blue:0.93 alpha:1.0];


    tourSetting = [[NSMutableArray alloc] initWithObjects:@"城市", @"日期", @"景点", @"语种",@"人数",nil];

    //template for selecting tabbar item

}

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

    return [tourSetting count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"toursetting"];

    cell.textLabel.text = [tourSetting objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(cell.textLabel.text);
    if([cell.detailTextLabel.text  isEqual: @""]){
        NSLog(@"is null");
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    if(indexPath.row == 0){
        UITableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"city"];
        [[self navigationController] pushViewController:viewController animated:YES];
    }
    else if(indexPath.row == 1){
        UITableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"data"];
        [[self navigationController] pushViewController:viewController animated:YES];
    }
    else if(indexPath.row == 2){
        UITableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"views"];
        [[self navigationController] pushViewController:viewController animated:YES];
    }
    else if(indexPath.row == 3){
        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"language"];
        [[self navigationController] pushViewController:viewController animated:YES];
    }
    else if(indexPath.row == 4){
        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"party"];
        [[self navigationController] pushViewController:viewController animated:YES];
    }


}

-(void)setTabBarItem:(UITabBarItem *)tabBarItem{

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

/********below are delegate method*****************/
-(void)done:(CityListTableViewController *)controllor selectedCity :(UITableViewCell*)citySelected{

    NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
    [setTourTableView cellForRowAtIndexPath:path].detailTextLabel.text = citySelected.textLabel.text;
    NSLog(@"dddd");
}

@end

这是整个代码,我没有使用segue从视图转移到另一个视图。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

你需要在这里纠正两件事:

  1. 只需将您的城市名称传递为NSString
  2. ,而不是传回单元格
  3. 在rootViewController中,在数据源中添加新城市。
  4. 重新加载表格。
  5. 这将简单快捷。