将UITableViewCell连接到Action

时间:2012-11-22 00:51:07

标签: iphone objective-c ios cocoa-touch uitableview

我正在尝试使用带有披露指示符的特定单元格来调出UIImagePicker(其他单元格有UITextFields等等...)到目前为止,我只能使用UIButton。我想将我的动作连接到单元格,但我唯一的选择是创建新的segues。我该怎么做?

3 个答案:

答案 0 :(得分:1)

如果您知道要单播的单元格的索引路径(它是列表中的第一个单元格或其他内容)。然后你可以使用:

-(void) tableview:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        // do some action
        [tableview deslectRowAtIndexPath:indexPath];
        [self someAction];
    }
}

您还希望将cellForRowAtIndexPath中的所有其他单元格设置为不响应点击事件。

答案 1 :(得分:1)

如果您不想对节和行的索引进行硬编码,可以执行以下操作:

<。>在.h文件中,使用IB创建IBOutlets

@property (weak, nonatomic) IBOutlet UITableViewCell *upgradeAppCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *spreadTheWordCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *rateOnAppStoreCell;
在.m文件中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (cell == upgradeAppCell)
    {
        [self upgradeApp];
    }
    else if (cell == spreadTheWordCell)
    {
        [self spreadTheWord];
    }
    else if (cell == rateOnAppStoreCell)
    {
        [self rateOnAppStore];
    }
}

答案 2 :(得分:0)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
   static NSString *identifier = @"identifier";
   YourCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
   if(!cell)
   {
       cell = /*Create your cell*/
       [cell.button addTarget:self action:@selector(didClickButton:)   forControlEvents:UIControlEventTouchUpInside];
   }

   cell.button.tag = [indexPath row];   

   return cell;
}

-(void)didClickButton:(UIButton *)button
{
    int row = button.tag
    /*Here you'll be able to know the button of which row has been clicked.*/
}