如何禁用tableview的特定行但特定行包含uibutton.按钮必须访问



我有UITableview

uitableview包含播放列表音频歌曲,我在单元格中放了一个按钮。

用户点击行音频开始播放

我想,当用户点击行按钮应该启用而单元格应该禁用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSURL *url = [[NSURL alloc]initFileURLWithPath:soundsPath];
    NSURL *  playurl =[NSURL URLWithString:[passarray objectAtIndex:indexPath.row] relativeToURL:url];
    if (indexPath.row ==1) {
        buybtn.userInteractionEnabled=YES;
        cell.contentView.userInteractionEnabled=YES;
        buybtn.showsTouchWhenHighlighted=YES;
  }
       player = [[AVAudioPlayer alloc] initWithContentsOfURL:playurl error:nil]; 
        [self.player play];
        player.delegate = self;
   cell =[tableView cellForRowAtIndexPath:indexPath];
       cellText = cell.textLabel.text;
    lbl.text=cellText;
     }

In method

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

为需要访问的按钮指定一个标签:buybtn.tag = YOUR_TAG

,然后在didselectrow方法中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"sunviews  : %@",cell.subviews);
    for (UIView *_view in cell.subviews) {
        if (_view.tag != YOUR_TAG) {
            [_view setUserInteractionEnabled:NO];
        }
    }
}

希望对你有帮助。

这可能会有所帮助:在UITableView's didSelectRowAtIndexPath:方法中添加这些代码

tag加到specific, button加到99,然后

for(UIView *subview in cell.subView)
{
   if(![subview isKindOfClass:[UIButton class]) 
   {
      subview.userInteractionEnabled = NO;
   }
   else
   {
      UIButton *btn = (UIButton *)subview;
      if(btn.tag != 99)
        btn.userInteractionEnabled = NO;
   }
}

EDIT: A specific button to enabled from other buttons .

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

if(indexPath.row == sepicificRowNumber){
buybtn.userInteractionEnabled=YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if(indexPath.row == sepicificRowNumber){
//Nothing happen on click   
}

}

希望有帮助。

最新更新