我在按下 ViewController 上的按钮以加载数组中的特定大量信息以便在 UITableViewController 上显示时遇到了一些麻烦。
我知道你可以通过检测 segue 来做到这一点,但我已经试过了 - 我成功了。我在其他地方读到如何使用按钮上的标签来识别它,我尝试使用它,但无论我按下什么按钮,它都只显示一组数据。我已经尝试交换很多东西来尝试让它工作但无济于事。我有一个带有按钮的 ViewController,一个用于显示数组的 TableViewController 和一个用于显示 tableview 信息的更好描述的 CellViewController。
目前,我的代码“遍地开花”。我没有包含 Cell View Controller ,因为我认为它们与我现阶段想要做的事情没有太大关系。buttonviewcontroller 只是一个普通的 UIViewController,两个按钮在 Storyboard中都链接为“Button1”类.他们被赋予了标签 0 和 1,我相信它们位于 Storyboard 中的“ View ”子标题下。如果这是错误的,请提醒我,我遗漏了一些非常明显的东西,但那是我唯一能找到“标签”的地方。
老实说,我不太清楚自己在 xcode 中做什么,因为我对它不是很熟悉,但似乎理解其中的一部分。但是,我不确定我现在做错了什么。
代码:
ButtonViewController.M(这是按钮所在的位置)
#import <UIKit/UIKit.h>
#import "tableViewController.h"
#import "TableCell.h"
@interface ButtonViewController : UIViewController
-(IBAction) button_Clicked:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *Button1;
@end
ButtonViewController.m
#import "ButtonViewController.h"
#import "tableViewController.h"
#import "TableCell.h"
@interface ButtonViewController ()
@end
@implementation ButtonViewController
-(IBAction) button_Clicked:(id)sender
{
//something here that is going wrong
tableViewController *tableVC = [[tableViewController alloc]initWithNibName:@"tableViewController" bundle:nil];
if(_Button1.tag==0)
{
tableVC.buttonSelected = 0;
}
else if(_Button1.tag==1)
{
tableVC.buttonSelected = 1;
}
[self.navigationController pushViewController:tableVC animated:YES];
[tableVC.tableView reloadData];
@end
tableViewController.h
#import <UIKit/UIKit.h>
@interface tableViewController : UITableViewController
@property (nonatomic, assign) int buttonSelected;
@property (nonatomic, strong) NSArray *Title;
//@property (nonatomic, strong) NSMutableArray *Title;
@property (nonatomic, strong) NSArray *Description;
//@property (nonatomic, strong) NSMutableArray *Description;
//Not sure if Mutable or normal array
@end
tableViewController.m
#import "tableViewController.h"
#import "TableCell.h"
#import "DetailViewController.h"
#import "ButtonViewController.h"
@interface tableViewController ()
@end
@implementation tableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(_buttonSelected == 0)
{
_Title = @[@"Hamstring Muscle Tear",@"Lower Back Pain"];
_Description = @[@"Blahahaha", @"blahahaha2",@"blalalala3"];
[self.tableView reloadData];
}
else if (_buttonSelected == 1)
{
_Title = @[@"1",@"2",@"3"];
_Description = @[@"dededdededde", @"deddedede2",@"blalalala3"];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _Title.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row];
cell.TitleLabel.text = _Title[row];
cell.DescriptionLabel.text = _Description[row];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailviewcontroller.DetailModal = @[_Title[row],_Description[row]];
}
}
@end
最佳答案
有两种方法可以做到这一点。
- 实现委托(delegate)方法并在事件发生时调用它。
- 您可以从一个 Controller 向另一个 Controller 触发 NSPostNotification 事件。
关于ios - 如何通过在不同的 View Controller 上按下按钮来更改 TableView 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575989/