【问题标题】:How to change tableview data with button press on a different view controller?如何通过在不同的视图控制器上按下按钮来更改 tableview 数据?
【发布时间】:2014-03-22 09:50:25
【问题描述】:

在 ViewController 上按下按钮以在数组中加载特定的大量信息以在 UITableViewController 上显示时遇到了一些麻烦。

我知道你可以通过检测 segue 来做到这一点,但我已经尝试过了——我成功了。我在其他地方读到了如何使用按钮上的标签来识别它,我尝试使用它,但无论我按下什么按钮,它都只显示一组数据。我尝试过交换很多东西以尝试使其正常工作,但无济于事。我有一个带有按钮的 ViewController,一个用于显示数组的 TableViewController 和一个用于显示对 tableview 信息的更好描述的 CellViewController。

目前,我的代码“遍布商店”。我没有包含单元视图控制器,因为我认为它们与我在这个阶段想要做的事情没有太大的相关性。buttonviewcontroller 只是一个普通的 UIViewController,两个按钮在情节提要中链接为“Button1”类.他们被赋予了标签 0 和 1,我相信它们位于情节提要的“视图”子标题下。如果这是错误的,请提醒我,我遗漏了一些非常明显的东西,但这是我唯一能找到“标签”的地方。

老实说,我不知道我在 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

【问题讨论】:

    标签: ios iphone objective-c arrays uitableview


    【解决方案1】:

    有两种方法可以做到这一点。

    1. 实现委托方法并在事件中调用它。
    2. 您可以将 NSPostNotification 事件从一个控制器触发到另一个控制器。

    【讨论】:

      【解决方案2】:

      更改数据,然后发布更改的NSNotification。

      表格视图数据源观察通知并调用[tableView reloadData]

      您可以在苹果的示例代码中找到详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-03
        相关资源
        最近更新 更多