将数据从 detailViewController 传回 UITableView [英] Passing data back from detailViewController to UITableView
问题描述
我目前遇到了需要更新的 UITableviewCell 的问题.
I am currently at the issue where I have a UITableviewCell that needs to be updated.
当用户按下 uitableviewcell - THERES ONLY 1!! 时,用户被推送到 UITABLEVIEWCONTROLLER,在那里用户可以选择具有自己标题的多个单元格中的 1 个.
When the user presses on the uitableviewcell - THERES ONLY 1!!, the user is pushed to a UITABLEVIEWCONTROLLER where the user is allowed to select 1 of multiple cells with their own titles.
我需要获取点击的 tableviewcells 标题并将值传递回 parentviewcontroller 并将 1 tableviewcell 的名称更新为用户在推送的 uitableivewcontroller 中点击的名称.
I need to get the clicked tableviewcells title and pass the value back to the parentviewcontroller and update the 1 tableviewcell's name to the one the user clicked in the pushed uitableivewcontroller.
这是父视图控制器的图片...
Here is a picture of the parent viewcontroller...
这是推送的视图控制器的图片....
And heres the picture of the pushed viewcontroller....
昨天早些时候有人告诉我需要授权,但我不确定此时该怎么办:/.
I was told earlier yesterday that delegation would be needed but I am unsure what to do at this point :/.
这是我在父视图控制器中使用的一些代码...
Heres some code I use in the parent viewcontroller...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ProblemTypes *view = [[ProblemTypes alloc] init];
[self.navigationController pushViewController:view animated:YES];
}
我也没有使用故事板,只是一些 xib.
I am also NOT USING storyboards, just a few xibs.
还有在选择单元格时pushviewcontroller弹出到父viewcontroller的代码...
Also heres the code for the pushedviewcontroller to pop to the parent viewcontroller when a cell is selected...
#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];
}
谢谢你们!
推荐答案
找到了.... 委托是解决方案... 希望它是最有效的!!!这是委托的代码.
Found it out.... Do delegation was the solution... Just hope it's the MOST EFFICIENT!!! Here's the code for delegation.
首先实现parentViewcontroller的delegate及其方法,还要确保给parentviewcontroller添加委托...
First, implement the delegate of the parentViewcontroller and it's methods, also make sure to add delegation to the 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
以使用此示例和我的项目 :)
Now go to - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for the use of this example and my project :)
- (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;
}
现在,给子视图控制器添加一个协议,让子视图控制器符合父视图控制器
Now, add a protocol to the childviewcontroller to allow the childviewcontroller to conform to the parentviewcontroller
在 childviewcontroller.h 中:添加这几行代码,
In childviewcontroller.h: add these lines of code,
#import "ParentViewController.h"
@protocol SendFeedBackDelegate;
@interface FeedbackTypes : UITableViewController
{
id<SendFeedBackDelegate> delegate;
}
@property (nonatomic, assign) id<SendFeedBackDelegate> delegate;
现在您已经在父视图控制器中设置了委托......接下来查看相同的文件实现文件 (.m) 并添加以下内容:
Now you have set the delegate in the parent viewcontroller.... Next head over the same files implementation file (.m) and add these:
//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];
}
就是这样!!!!..... :),希望这是一个简单而基本的教程,这里是一个快照.
And THATS IT!!!!..... :), Hope this was a simple and basic tutorial and here's a snapshot.
这篇关于将数据从 detailViewController 传回 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!