是否可以在自定义 UITableViewCell 中有一个按钮,该按钮引用其中的选择器

Posted

技术标签:

【中文标题】是否可以在自定义 UITableViewCell 中有一个按钮,该按钮引用其中的选择器【英文标题】:Is it possible to have a button in a custom UITableViewCell that references a selector in it 【发布时间】:2014-08-25 18:41:40 【问题描述】:

我不是全职的 ios 开发人员,所以这可能很简单。我在自定义 UITableViewCell 中有以下内容,但它似乎不起作用。绝大多数的选择器都在控制器中。这可能吗?我在这里做错了什么?

在我的自定义 UITableViewCell 中

-(void)updateCell:(NSDictionary *)content

  
  UILabel *mainLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0f, 20.0f)];
  mainLabel.text=[content objectForKey:@"name"];
  [self.contentView addSubview:mainLabel];
  
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button addTarget:self
             action:@selector(aMethod:)
   forControlEvents:UIControlEventTouchUpInside];
  [button setTitle:@"Show View" forState:UIControlStateNormal];
  button.frame = CGRectMake(0.0f, 20.0f, 50.0f, 20.0f);
  [self.contentView addSubview:button];


-(void)aMethod:(id)sender

  NSLog(@"here is a method");

在我的 ViewController 中的 cellForRowAtIndexPath 中调用。

编辑

基本上我想得到类似这样的扩展内容效果:http://jsfiddle.net/bso3sa07/

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

ViewController.m

#import "ViewController.h"
#import "ContentCell.h"

@interface ViewController ()
  NSMutableArray *_data;
  


@end

@implementation ViewController

- (void)viewDidLoad

    [super viewDidLoad];
  
  
  self.tableView.dataSource=self;
  self.tableView.delegate=self;
  
  NSDictionary *obj1=@@"name":@"Julie";
  NSDictionary *obj2=@@"name":@"Melissa";
  
  _data=[[NSMutableArray alloc] init];
  [_data addObject:obj1];
  [_data addObject:obj2];
  


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

  return [_data count];


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

  id tmpCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  ContentCell *cell=(ContentCell *)tmpCell;
  return cell.height;


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  static NSString *CellIdentifier=@"ContentCell";
  ContentCell *cell = (ContentCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  cell = [[ContentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  [cell updateCell:[_data objectAtIndex:indexPath.row]];
  
  
  UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [moreButton addTarget:self
                 action:@selector(vcSelector)
       forControlEvents:UIControlEventTouchUpInside];
  [moreButton setTitle:@"vcMore" forState:UIControlStateNormal];
  moreButton.frame = CGRectMake(200.0f, 10.0f, 100.0f, 20.0f);
  [cell.contentView addSubview:moreButton];
  
  
  return cell;


-(void)vcSelector

  NSLog(@"hello from vcSelector");
  // lets get a reference to that specific cell
  // how would I expand that specific cell?
  /*
   id tmpCell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
   ContentCell *cell=(ContentCell *)tmpCell;
   return cell.height;
   */
  





- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

ContentCell.h

#import <UIKit/UIKit.h>

@interface ContentCell : UITableViewCell

@property (nonatomic, assign) CGFloat height;
-(void)updateCell:(NSDictionary *)obj;
@end

ContentCell.m

#import "ContentCell.h"



@implementation ContentCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
        // Initialization code
    
    return self;


- (void)awakeFromNib

    // Initialization code


-(void)updateCell:(NSDictionary *)content

  self.height=60.0f;
  UILabel *mainLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0f, 20.0f)];
  mainLabel.text=[content objectForKey:@"name"];
  mainLabel.layer.borderColor=[UIColor greenColor].CGColor;
  mainLabel.layer.borderWidth=2.0f;
  [self.contentView addSubview:mainLabel];
  
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button addTarget:self
             action:@selector(aMethod:)
   forControlEvents:UIControlEventTouchUpInside];
  [button setTitle:@"Show View" forState:UIControlStateNormal];
  button.frame = CGRectMake(0.0f, 20.0f, 100.0f, 20.0f);
  [self.contentView addSubview:button];
  



-(void)aMethod:(id)sender

  NSLog(@"here is a method");
  UIView *pushView=[[UIView alloc] initWithFrame:CGRectMake(10.0f, 50.0f, 300.0f, 5.0f)];
  [pushView setBackgroundColor:[UIColor redColor]];
  [self.contentView addSubview:pushView];
  self.height=90.0f;
  



- (void)setSelected:(BOOL)selected animated:(BOOL)animated

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state


@end

【问题讨论】:

是的,这完全有可能,而且调试时间不会太长。你想要每个单元格上的按钮吗? 是的,我会 - 我认为我的选择器设置不正确 在上面的代码中,您调用的方法位于自定义单元格对象内。您是要在单元格对象中执行代码,还是要在表格视图中执行某些功能?您能否详细说明该方法的用途? 向我们展示 cellForRowAtIndexPath 中的代码可能会有所帮助,这可能是一个重用的东西,但不太确定。 @selector 似乎设置正确,据我所知。话虽如此,我现在无法测试它。但我认为@selector 没有问题。 【参考方案1】:

试试这样的:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    // Check if button exist already
    UIButton *button = (UIButton*)[cell viewWithTag:555];
    // if not - create new one
    if (!button)
    
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.tag = 555;
        button.frame = CGRectMake(0.0f, 20.0f, 50.0f, 20.0f);
        [cell addSubview:button];
    

    // update title and selector if required
    [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Show View" forState:UIControlStateNormal];

    return cell;

【讨论】:

以上是关于是否可以在自定义 UITableViewCell 中有一个按钮,该按钮引用其中的选择器的主要内容,如果未能解决你的问题,请参考以下文章