gpt4 book ai didi

iphone - ECSlidingViewController 推送数据到 TableView

转载 作者:行者123 更新时间:2023-11-29 11:01:13 26 4
gpt4 key购买 nike

我正在尝试使用 github 上提供的 ECSlidingViewController:

https://github.com/edgecase/ECSlidingViewController

无论如何,我想知道如何将数据从菜单 View Controller 推送到示例 TableView Controller ,因为没有 segue。导航 Controller 通过 storybaord 实例化,但我看不到将任何数据从 View 推送到另一个 View 的方法。

我现在尝试使用代理,但似乎我的代理因为某种我不知道的原因没有解雇。 TableView 保持为空。我错过了什么?

请看下面的代码:

//MenuviewController.h

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

@protocol TableData <NSObject>

- (void) someData:(NSString*)data;

@end

@interface MenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate>{

__unsafe_unretained id <TableData> delegate;
}

@property (nonatomic,assign) id <TableData> delegate;

@end

//MenuViewController.m

#import "MenuViewController.h"

@interface MenuViewController()
@property (nonatomic, strong) NSArray *menuItems;
@end

@implementation MenuViewController
@synthesize menuItems, delegate;

- (void)awakeFromNib
{
self.menuItems = [NSArray arrayWithObjects:@"First", @"Second", @"Third", @"Navigation", nil];
}

- (void)viewDidLoad
{
[super viewDidLoad];

[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;



}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"%@Top", [self.menuItems objectAtIndex:indexPath.row]];

UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

if ([delegate respondsToSelector:@selector(someData:)]) {
[delegate someData:@"Hello World"];
}

[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
CGRect frame = self.slidingViewController.topViewController.view.frame;
self.slidingViewController.topViewController = newTopViewController;
self.slidingViewController.topViewController.view.frame = frame;
[self.slidingViewController resetTopView];
}];
}

@end

//SampleTableViewController.h

#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"

@interface SampleTableViewController : UITableViewController <UITableViewDataSource, UITabBarControllerDelegate, TableData>

- (IBAction)revealMenu:(id)sender;

@end

//SampleTableViewController.m

#import "SampleTableViewController.h"

@interface SampleTableViewController()
@property (nonatomic, strong) NSArray *sampleItems;
@end

@implementation SampleTableViewController
@synthesize sampleItems;

- (void)awakeFromNib
{
//self.sampleItems = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
}

- (void) someData:(NSString *)data{

sampleItems = [NSArray arrayWithContentsOfFile:data];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.sampleItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"SampleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

cell.textLabel.text = [self.sampleItems objectAtIndex:indexPath.row];

return cell;
}

- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}

@end

最佳答案

我建议使用委托(delegate)。这是一个例子:Delegate example

关于iphone - ECSlidingViewController 推送数据到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15839401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com