【问题标题】:Custom table view cell with button带按钮的自定义表格视图单元格
【发布时间】:2015-09-03 06:16:53
【问题描述】:

我正在使用故事板。我有一个带有 1 个原型单元格的表格视图,并且在表格视图中有四行。每个单元格都有一个标签、图像视图和一个按钮。现在我想在下一个视图控制器上显示不同的数据,比如(DetailsVc),每次按下按钮时。 Row1- 按钮单击将打开带有一些数据的 DetailsVC。 Row2- 按钮单击将打开具有不同数据的 DetailsVC。 其余行的相同过程。 任何建议。

@implementation DetailsVC

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
EmpEngmntTableViewCell *emp;

 if((emp.viewGalleryBtn.tag = 1))
{
    NSLog(@"tag num for 1st row is %ld",(long)emp.viewGalleryBtn.tag);
    self.title = @"Team Building Session";
}
}

【问题讨论】:

  • 无法理解你做了什么。您能否为didSelectRowAtIndexPathDetailVC 添加更多代码?
  • 其实didSelectRowAtIndexPath中我并没有添加任何代码。 DetailsVC 是下一个视图控制器,它将在单击自定义表格视图单元中的前一个视图控制器中的按钮时打开。我已经与 DetailsVC 建立了按钮的推送关系。现在我想要的是,Row1- 按钮单击将打开带有一些数据的 DetailsVC。 Row2- 按钮单击将打开具有不同数据的 DetailsVC。其余行的处理相同
  • 那么您需要将数据从第一个 Viewcontroller 传递到 DetailVC。如果不传递数据,您将无法在 DetailVC 中获取数据。参考stackoverflow.com/questions/8429088/…stackoverflow.com/questions/11535915/…
  • 现在在 EventDetailViewController 类中检查 buttonTag 并进行条件化

标签: ios objective-c storyboard


【解决方案1】:

最好的方法是使用委托模式。我添加了一个答案here,你可以用它来举例。

下面是一个例子:

自定义单元格 .h 类:

@protocol DetailVCProtocol <NSObject>
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex;
@end

@property (nonatomic, retain) id<DetailVCProtocol> delegate;
@property (nonatomic, retain) IBOutlet UIButton *btn;

然后合成到.m文件中

将此添加到 m 文件

-(IBAction)loadDetails:(id)sender
{
    // ..... blah blah
    [self.delegate loadDetailScreenWithIndex:btn.tag];
}

加载此单元格的视图控制器:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // create cell here

   cell.btn.tag = indexPath.row;
   cell.delegate = self;
}

-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex
{
  // Cretae DetailVC with parameter `rowIndex` to populate data
  [self presentViewController:detailVC animated:YES completion:nil];
}

【讨论】:

    【解决方案2】:

    在函数- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    使用此代码

    cell.btn.tag = indexPath.row;
    [cell.btn addTarget:self action:@selector(btnTpd:) forControlEvents:UIControlEventTouchUpInside];
    
    - (IBAction)btnTpd:(id)sender
    {
    UIButton *btn = (UIButton *)sender;
    \\You can get which button is pressed by btn.tag
        \\Here call to next ViewController and pass data
    }
    

    【讨论】:

    • 不要使用这个代码来回答你的问题,它真的很糟糕。
    【解决方案3】:

    在 cellForRowAtIndexPath 中

     cell.optionsBtn.tag=indexPath.row;
     [cell.optionsBtn addTarget:self action:@selector(showMyOptionBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    -(void)showMyOptionBtn:(UIButton *)sender
    {
      EventDetailViewController *EventDetail=[[EventDetailViewController alloc]init];
      EventDetail.buttonTag=sender.tag;
      [self.navigationController pushViewController:EventDetail animated:NO];
    
     }
    
    In EventDetailViewController class
    
       if (self.buttonTag==1) {
         // your condation
       }
       else
       {
        //your condation
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多