使用委托从按钮传递信息以突出显示单元格 [英] Using a delegate to pass information from button to highlight cell

查看:137
本文介绍了使用委托从按钮传递信息以突出显示单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父视图控制器"和一个子视图控制器",我需要将信息从子视图控制器"中的按钮传递回父视图控制器",以便突出显示正确的tableViewCell文本./strong>

在父视图控制器"中,点击一个单元格时,该单元格文本将突出显示,并且子视图控制器"会在屏幕上弹出并播放所选的歌曲.但是,在子视图"控制器上有一个向后跳过"和向前跳过"按钮,因此,当按下其中任一按钮并再次显示父视图"控制器时,现在需要突出显示不同于其他单元格的单元格文本之前.

因此,我已经向每个视图控制器中添加了适当的委托样板代码,但是我不确定从子视图控制器中获取要发送回父视图控制器的最佳信息,以及如何使用它./p>

儿童视图控制器:

-(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; 
    } 
}

我会在上述每个switch语句中添加一些内容,但是我不确定哪种方法最有效?

因为这是 Parent View Controller (父视图控制器)中的样子:

- (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;
}

我知道我会添加某种方法并重新加载表,但是我不确定执行此操作的最佳方法.有任何想法吗?谢谢!

解决方案

  1. 在子控制器中定义协议.该协议应具有类似"didSelectSkipButton"的方法,该方法接受一些参数来表示按下的按钮的类型.

  2. 为子视图控制器定义一个委托属性,符合协议.

  3. 在孩子的indexChanged中,将didSelectSkipButton消息发送给委托,并传递代表按下按钮的值.

  4. 在父级vc中,将其自身设置为子级的委托并实现协议方法.在didSelectSkipButton中,使用indexPathForSelectedRow查找表视图的当前行,并根据所按下的按钮从[indexpath row]中添加或减去一个.检查新行索引是否在表视图的有效范围内.然后发送您的表格视图 selectRowAtIndexPath:animated:scrollPosition:消息以选择新行.

I have a Parent View Controller and a Child View Controller, and I need to pass information from a button in the Child View Controller back to the Parent View Controller so that the correct tableViewCell text is highlighted.

In the Parent View Controller when a cell is tapped, the cell text is highlighted, and the Child View Controller pops on screen and plays the song that was selected. However, on the Child View Controller there is a skip backward and skip forward button, so when either of those is pressed and the Parent View Controller is shown again the cell text needs to be highlighted for a different cell now than was highlighted before.

So I've added the proper delegate boilerplate code to each view controller, but I'm not sure the best information to grab from the Child View Controller to send back to the Parent View Controller, and how to use that.

Child View Controller:

-(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; 
    } 
}

I would add something inside of each of the above switch statements, but I'm not sure what exactly would work best?

Because this is what it looks like in the Parent View Controller:

- (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;
}

I know I'd add some sort of method, and reload the table, but I'm not sure the best way to go about this. Any ideas? Thanks!

解决方案

  1. Define a protocol in your child controller. This protocol should have a method like 'didSelectSkipButton' that accepts some parameter to represent the type of button pressed.

  2. Define a delegate property for your child view controller, conforming to the protocol.

  3. In the child's indexChanged, send the didSelectSkipButton message to the delegate, passing the value representing the pressed button.

  4. In your parent vc, set itself up as the child's delegate and implement the protocol methods. In didSelectSkipButton, find the table view's current row using indexPathForSelectedRow and add or subtract one from [indexpath row] depending on the button pressed. Check that the new row index is within the valid bounds for your table view. Then send your table view a selectRowAtIndexPath:animated:scrollPosition: message to select the new row.

这篇关于使用委托从按钮传递信息以突出显示单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆