一个表视图的多个按钮



有人可以指出我正确的方向吗?我有一个带有多个按钮的视图控制器。我希望按下按钮打开我的自定义表视图控制器并动态加载数据。我知道我可以为每次按下按钮制作多个表视图控制器,但我认为必须有一种方法可以动态地执行此操作。让我明确一点,另一个视图将确定将哪些数据加载到表视图控制器中。我是iOS和Objective-C的新手,但不是编程,所以在火箭科学答案上放轻松。如果您需要查看一些代码,请告诉我。

基本上,我在一个视图控制器上有 4 个按钮,按下这些按钮将确定要在表视图上加载哪些数据。让我们暂时保持简单,只说数据是 4 个 NSMutable 阵列。

编辑:根据babygau的帖子,这就是我想出的:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"allCattleSegue"]) {
        CattleTableViewController *controller = segue.destinationViewController;
        controller.cattleArray = [[NSMutableArray alloc] initWithObjects:@"cattle1",@"cattle2",@"cattle3", nil];
    }
    else if ([segue.identifier isEqualToString:@"bullsSegue"]){
        CattleTableViewController *controller = segue.destinationViewController;
        controller.cattleArray = [[NSMutableArray alloc] initWithObjects:@"bull1",@"bull2",@"bull3", nil];
    }
}
- (IBAction)allCattleTouchUpInside:(UIButton *)sender {
    [self performSegueWithIdentifier:@"allCattleSegue" sender:self];
}
- (IBAction)bullsTouchUpInside:(UIButton *)sender {
    [self performSegueWithIdentifier:@"bullsSegue" sender:self];
}

这样可以完成工作,但是现在当我按下后退按钮时,我的导航无法正常工作。它给了我一个空白的屏幕。

更新:调试我看到它调用了 prepareForSegue 两次; 一次是控制器作为发送者,另一次是按钮作为发送者。仍在尝试了解其工作原理以及如何解决此导航问题。

已解决:显然我不需要连接操作(TouchUpInside)。感谢所有的帮助。

嗨,

只需创建一个Viewcontroller和TableViewController。设置所有按钮的标签,然后在 TableViewController 中创建一个 @property int 变量,然后像下面这样做。

创建通用方法来触发UIControlTouchUpInside中的所有按钮。然后在UITableViewController中创建这样的int变量。

TableViewController.h 文件是你像这样创建的。

@property(非原子,分配)int 按钮已选中;

ViewController.m 创建这样的常用方法。

-(IBAction)commonMethod:(id)sender
{
TableViewController *tableVc = [[TableViewController alloc]initWithNibName:@"TableViewController" bundle:nil];
if(button.tag==0)
{
 tableVC.buttonSelected = 0;
}
if(button.tag==1)
{
tableVC.buttonSelected = 1;
}
[self.navigationController pushViewController:tableVc animated:YES];

}

TableViewConroller.m 文件执行此操作

-(void)viewDidLoad
{
[super viewDidLoad];
if(buttonPressed == 0)
{
tableDataSourceArray = ...
}
else if (buttonPressed == 1)
{
tableDataSourceArray = ...
}
}

希望对您有所帮助。

假设你的表有一个数组tableViewData

在每个按钮操作中,使用以下模式代码:

[self performSegueWithIdentifier:@"YOUR_IDENTIFIER" target:sender];

实现以下 segue 委托方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    YOURCUSTOMTABLEVIEWCONTROLLER *tableVC= segue.destinationViewController;
    tableVC.tableViewData = YOUR_CORRESSPONDING_MUTABLE_ARRAY;
    [tableVC.tableView reloadData];
}

就是这样,您将根据您单击的按钮动态显示 tableView。

注意:您必须在视图控制器之间创建 4 个对应于 4 个按钮的 segue,并为每个 segue 命名标识符,例如 @"fromButton1toTableVC"、@"fromButton2toTableVC" 等等...

您可以传递诸如按钮标签或标识符之类的内容,以告知要使用哪个可变数组。

如果您在 prepareForSegue 方法中使用 segue 在代码中进行转换,则可以将值传递给目标 TableViewController,即

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
        UITableViewController *controller = segue.destinationViewController;
        controller.buttonTitle = sender;
}

然后在按钮处理程序中

[self perfomSegue:@"segue" sender:buttonTitle];
为每个

按钮分配标签。根据标签,您可以在表中填充不同的数据。只需只使用一个数组。根据标签将数据填充到该数组

if(button.tag==0)
{
//array=@[..];
}
if(button.tag==1)
{
//array=@[..];
}
.
.
.

现在在委托方法中使用此数组cellForRowWithIndexPath

最新更新