【问题标题】:Passing data back from detailViewController to UITableView将数据从 detailViewController 传回 UITableView
【发布时间】:2013-06-27 16:18:22
【问题描述】:

我目前遇到的问题是我有一个需要更新的 UITableviewCell。

当用户按下 uitableviewcell - THERES ONLY 1!! 时,用户将被推送到 UITABLEVIEWCONTROLLER,用户可以在其中选择具有自己标题的多个单元格中的 1 个。

我需要获取单击的 tableviewcells 标题并将值传递回 parentviewcontroller 并将 1 tableviewcell 的名称更新为用户在推送的 uitableivewcontroller 中单击的名称。

这是父视图控制器的图片...

这是推送的视图控制器的图片....

昨天早些时候有人告诉我需要委托,但我现在不确定该怎么做:/。

这是我在父视图控制器中使用的一些代码...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ProblemTypes *view = [[ProblemTypes alloc] init];
    [self.navigationController pushViewController:view animated:YES];

}

我也没有使用故事板,只是一些 xib。

在选择单元格时,SuppedViewController的代码将弹出到父ViewController ...

#pragma mark - Table view delegate

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"Cell's text: %@",cell.textLabel.text);

    [self.navigationController popViewControllerAnimated:YES];
}

谢谢你们!

【问题讨论】:

    标签: uinavigationcontroller uitableview segue


    【解决方案1】:

    发现了....委托是解决方案...只是希望它是最有效的!!!这是委托的代码。

    首先,实现parentViewcontroller的delegate及其方法,还要确保将delegation添加到parentviewcontroller...

    @protocol SendFeedBackDelegate
    
    - (void) didReceiveType:(NSString *) message;
    
    @end
    @interface SendFeedBackViewController : UIViewController <SKPSMTPMessageDelegate, UITableViewDataSource,UITableViewDelegate, SendFeedBackDelegate>
    {
        NSString *subject;
    
    }
    

    接下来,实现方法:- (void) didReceiveType:(NSString *) message;

    @implementation SendFeedBackViewController
    
    - (void) didReceiveType:(NSString *) message
    {
        subject = message;
        [feedbackTableView reloadData];
        // I reload the data because it is needed when this function is going to be called
        // in the child viewcontroller.... just keep reading :)
    }
    

    现在去- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用这个例子和我的项目:)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [feedbackTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        // Configure the cell...
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        // THIS IS THE IMPORTANT PIECE OF CODE YOU NEED TO NOTICE.....
        // it allows for the first thing the tableview cell to be is a static string until subject 
        // it is changed and the user chooses a subject in the childviewcontroller
        if (subject == nil) {
            cell.textLabel.text = @"Select a Product";
        } else {
            cell.textLabel.text = subject;
        }
    
    
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        return cell;
    }
    

    现在,给 childviewcontroller 添加一个协议,让 childviewcontroller 符合 parentviewcontroller

    在childviewcontroller.h中:添加这几行代码,

    #import "ParentViewController.h"
    
    @protocol SendFeedBackDelegate;
    
    @interface FeedbackTypes : UITableViewController
    {
        id<SendFeedBackDelegate> delegate;
    }
    
    @property (nonatomic, assign) id<SendFeedBackDelegate> delegate;
    

    现在您已经在父视图控制器中设置了委托......接下来在相同的文件实现文件(.m)上添加这些:

    //Add synthesize just under @implementation "ClassName"
    
    @synthesize delegate;
    
    // I used a uitableviewcontroller for this example so refer to the problem I have above
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        //NSLog(@"Cell's text: %@",cell.textLabel.text);
        [delegate didReceiveType:cell.textLabel.text];
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    就是这样!!!!..... :),希望这是一个简单而基本的教程,这是一个快照。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多