-->

使用委托,从按钮高亮细胞传递信息(Using a delegate to pass informat

2019-11-04 07:04发布

我有一个父视图控制器和一个子视图控制器,我需要让正确的儿童视图控制器从一个按钮将信息传递回父视图控制器tableViewCell文本高亮显示。

在父视图控制器,当细胞被窃听,单元格文本高亮显示,儿童视图控制器弹出在屏幕上,并播放了所选择的歌曲。 然而,在儿童视图控制器有一个快退和快进按钮,当按下时,无论是那些与父视图控制器再次显示单元格文本需要现在比以前的重要性被突出显示为不同的小区

所以,我已经添加了适当的委托样板代码到每个视图控制器,但我不知道最好的信息从子视图控制器抢发回父视图控制器,以及如何使用。

子视图控制器

-(IBAction)indexChanged:(UISegmentedControl *)sender
{
    NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Blip_Select" ofType:@"wav"]];
    AVAudioPlayer *click  = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile  error:nil];

    switch (self.segmentedControl.selectedSegmentIndex)
    {
        case 0:
            [click play];
            [click play];
            [musicPlayer skipToPreviousItem];
            break;
        case 1:
            [click play];
            if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
                [musicPlayer pause];
            } else {
                [musicPlayer play];
            }
            break;
        case 2:
            [click play];
            [click play];
            [musicPlayer skipToNextItem];
        default:
            break; 
    } 
}

我想补充一点,在上述各开关语句里面,但我不知道究竟会效果最好

因为这是它看起来像父视图控制器

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

    MPMediaItem *rowItemAlbum = [[albumsArrayForTVC objectAtIndex:indexPath.section] representativeItem];
    NSString *albumDetailTitle = [rowItemAlbum valueForProperty:MPMediaItemPropertyAlbumTitle];
    MPMediaQuery *albumMPMediaQuery = [MPMediaQuery albumsQuery];
    MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: albumDetailTitle forProperty: MPMediaItemPropertyAlbumTitle];
    [albumMPMediaQuery addFilterPredicate:albumPredicate];
    NSArray *albumTracksArray = [albumMPMediaQuery items];

    MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:indexPath.row] representativeItem];
    NSString *songTitle = [rowItemSong valueForProperty:MPMediaItemPropertyTitle];
    cell.textLabel.text = songTitle;
    cell.textLabel.highlightedTextColor = [UIColor brownColor];

    return cell;
}

我知道我要补充某种方法,并重新加载表,但我不知道去了解这一点的最好办法。 有任何想法吗? 谢谢!

Answer 1:

  1. 定义你的孩子控制器的协议。 该协议应当具有像“didSelectSkipButton”的方法,其接受一些参数来表示按钮的按压类型。

  2. 定义委托物业为您的孩子视图控制器,符合协议。

  3. 在孩子的indexChanged,发送didSelectSkipButton消息委托,通过代表按下按钮的价值。

  4. 在你的父母VC,把自己定为孩子的委托并实现协议的方法。 在didSelectSkipButton ,使用查找表视图的当前行indexPathForSelectedRow以及添加或从一个减去[indexpath row]根据按下的按钮。 检查新行指数是你的表视图中的有效范围内。 然后把你的表查看selectRowAtIndexPath:animated:scrollPosition:消息来选择新行。



Answer 2:

在你的孩子视图控制器创建委托

 @protocol ChildView_Delegate <NSObject>
 -(void)reSelectTableRow:(int)index
 @end

在你的孩子视图控制器创建属性

@property (nonatomic) id delegate;

而当你按下正反向按钮做这样的事情。

-(IBAction)forward:(id)sender{
   if(_delegate){
      [_delegate reSelectTableRow:position+1];
   }
}

回到你的主控制器实现ChildView_Delegate和实现它的方法是这样

 -(void)reSelectTableRow:(int)index{
   if(index<songsArray.count){
     //define a index path
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index  inSection:0];
     //select a row of table view programatically
     [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:
     UITableViewScrollPositionNone];
   }
 }


Answer 3:

添加委托方法在父母

 @protocol childDelegate <NSObject>
 -(void) updateTable
 @end

@property (assign) id <childDelegate> cDelegate;

然后添加在父母同样的方法

-(void) updateTable
{
  [yourTableView reloadData];
}

-(IBAction)indexChanged:(UISegmentedControl *)sender
{
  [self.cDelegate updateTable];
}

希望能帮助到你。



文章来源: Using a delegate to pass information from button to highlight cell