我如何在tableview中创建爱/不爱项目的按钮

时间:2016-04-17 12:16:43

标签: objective-c

我是学生。抱歉我的英语,因为它非常糟糕。 我有作业。 我正在研究Obj-C 制作卡拉OK应用程序(级别简单) Buttons LOVE some musics ( image here)

Alert will show : Do you want to add this song to Tab bar : My Love Song( image here)

我的问题:如何在tableview中创建爱/不爱项目的按钮? 请帮我 !谢谢大家!

1 个答案:

答案 0 :(得分:1)

创建自定义UITableViewCell并设置2个按钮,然后在设置单元格时,例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];

    // Configure the cell...
    cell.likeToggleButton.selected = NO; //--> should be based on your settings
    [cell.likeToggleButton addTarget:self action:@selector(toggleAction:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}


- (void)toggleActio:(UIButton *)sender {

    UITableViewCell *cell = (UITableViewCell *)[sender.superview superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    if (sender.selected) {
        // perform your dislike action based on button indexPath
        sender.selected = NO;
    }else {
         // perform your like action based on button indexPath
         sender.selected = YES;
    }

}