将静态单元与动作连接

时间:2013-03-29 11:07:51

标签: ios objective-c storyboard

我想使用故事板将静态单元格与动作连接起来。问题是你无法通过动作连接单元格,所以我尝试了另一种方式。 所以在我的头文件中,我使用故事板将此属性与静态单元连接起来:

@property (nonatomic, strong) IBOutlet UITableViewCell *theStaticCell;  

    UITableViewCell *theCellClicked = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
if (theCellClicked == _theStaticCell) {
    NSLog(@"Static cell clicked");
}

所以我想使用“更新”单元格,当你点击它时,上面的代码就会被执行。

enter image description here

3 个答案:

答案 0 :(得分:15)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.section ==1 && indexPath.row == 0)
    {
        //Do what you want to do.
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Cell will be deselected by following line.
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UITableViewCell *staticCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];

    if (cell == staticCell)
    {
        //Do what you want to do.
    }
}

答案 1 :(得分:3)

我认为不应该使用静态单元格连接它,而应使用TableView Delegate Method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Put your action logic here by using its index "indexPath.row".
}

答案 2 :(得分:1)

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([tableView cellForRowAtIndexPath:indexPath] == self.theStaticCell){
        NSLog(@"Static cell clicked");
    }
}