通过其他按钮隐藏单元格中的按钮按uitableview

时间:2014-12-12 06:40:37

标签: ios objective-c uitableview cell

想知道如何在UITableView中获取每个单元格的所有项目。我的问题是,当我选择第三个时,我想在单元格中隐藏2个按钮。

example of my cell

当我按下按钮1时,必须隐藏按钮2和3。我试图做的(在cellForRowAtIndexPath中):

AVMMovieButton *settings = (AVMMovieButton *)[cell viewWithTag:228];
[settings addTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
settings.tag = indexPath.row;

AVMMovieButton *playButton = (AVMMovieButton *)[cell viewWithTag:134];
[playButton setStore:oneItem];
[playButton addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
playButton.tag = indexPath.row;

AVMMovieButton *down = (AVMMovieButton *)[cell viewWithTag:282];

AVMMovieButton *del = (AVMMovieButton *)[cell viewWithTag:161];

[settings  setSelected:!settings.isSelected];
if (settings.isSelected) 
{
    NSLog(@"SELECTED!");

    down.hidden = YES;
    del.hidden = YES;
    downLabel.hidden = YES;
    delLabel.hidden = YES;

   // [self performSelector:@selector(playButtonShow) withObject:nil afterDelay:0.3];
    playButton.hidden = NO;     
}
else
{
    NSLog(@"UNSELECTED!");

    playButton.hidden = YES;
    down.hidden = NO;
    del.hidden = NO;
    downLabel.hidden = NO;
    delLabel.hidden = NO;

    NSLog(@"play button %d",playButton.hidden);
}

然后我添加了选择我的"设置的方法"按钮:

-(void)selectSettings:(AVMMovieButton *)sender
{
    [sender setSelected:!sender.isSelected];
    NSLog(@"you just select button");
}

但它不起作用!

实际上NSLog(@"you just select button");有效,但按钮永远不会隐藏。

如何获取按钮并隐藏它们?

解决:

我需要做的就是创建自定义UITableViewCell课程,然后访问我的单元格,Jay Gajjar和Akhilrajtr说。在我使用我的方法选择/取消选择我的按钮之后。

我得到了什么:

-(void)selectSettings:(AVMMovieButton *)sender
{
    AVMMovieButton *settings = (AVMMovieButton *)sender;
    CGPoint pointInTable = [settings convertPoint:settings.bounds.origin toView:readyTable];
    NSIndexPath *indexPath = [readyTable indexPathForRowAtPoint:pointInTable];
    AVMMovieCell *cell=(AVMMovieCell *)[readyTable cellForRowAtIndexPath:indexPath];
    if (cell.settingsButton.isSelected) 
    {
        NSLog(@"SELECTED!");

        cell.downloadButton.hidden = YES;
        cell.deleteButton.hidden = YES;
        cell.downLabel.hidden = YES;
        cell.delLabel.hidden = YES;
        cell.playButton.hidden = NO; 
    }
    else 
    {
        NSLog(@"UNSELECTED!");
        cell.playButton.hidden = YES;
        cell.downloadButton.hidden = NO;
        cell.deleteButton.hidden = NO;
        cell.downLabel.hidden = NO;
        cell.delLabel.hidden = NO;
        NSLog(@"play button %d",cell.playButton.hidden);
    }
    [settings setSelected:!settings.isSelected];
}

希望这对其他人有帮助!

4 个答案:

答案 0 :(得分:3)

试试这个,

-(void)selectSettings:(AVMMovieButton *)sender{

    [sender setSelected:!sender.isSelected];
    CGPoint pointInTable = [sender convertPoint:sender.bounds.origin toView:_tableView];    
    NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
    [_tableView beginUpdates];
    [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [_tableView endUpdates];
    NSLog(@"you just select button");
}

答案 1 :(得分:2)

重新加载Table时会调用

cellForRowAtIndexPath。在您的情况下,您需要做的是:

- (void)button3Method:(id)sender
{
    // Begin UITableView Update
    [self.tableView beginUpdates];

    NSIndexPath *indexPath = // Get cell indexpath for the selected row.

    // Logic for getting the UITableViewCell on which you want hide two buttons.
    // Logic for hiding those two buttons

    // Below line performs reloading of particular UITableViewCell.
    [self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];

    // End UITaleView Update
    [self.tableView endUpdates];
}

如果这适合您,请告诉我。祝你好运。

答案 2 :(得分:1)

在cellForRowAtIndexPath中:

[cell. settings addTarget:self action:@selector(selectSettings:) forControlEvents:UIControlEventTouchUpInside];
cell. settings.tag=600+indexPath.row;

按钮IBAction:

-(void) selectSettings:(id) sender{    
    UIButton *btn=(UIButton *)sender;
    cellMoreButtonIndex=btn.tag;
    YOURCELL *cell=(YOURCELL *)[self.contentTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:btn.tag-600 inSection:0]];
[settings  setSelected:!settings.isSelected];
if (settings.isSelected) {
    NSLog(@"SELECTED!");

    down.hidden = YES;
    del.hidden = YES;
    downLabel.hidden = YES;
    delLabel.hidden = YES;

   // [self performSelector:@selector(playButtonShow) withObject:nil afterDelay:0.3];
    playButton.hidden = NO;

}else {
    NSLog(@"UNSELECTED!");


    playButton.hidden = YES;
    down.hidden = NO;
    del.hidden = NO;
    downLabel.hidden = NO;
    delLabel.hidden = NO;

    NSLog(@"play button %d",playButton.hidden);

}
}

答案 3 :(得分:1)

添加自定义单元格和处理按钮单击事件,并在单元级别隐藏/取消隐藏功能