使用委托从button传递信息突出显示单元格

我有一个父视图控制器和一个子视图控制器,我需要将信息从子视图控制器中的button传回到父视图控制器,以便正确的tableViewCell文本被高亮显示。

在父视图控制器中,单击单元格时,单元格文本将突出显示,并且子视图控制器会在屏幕上popup并播放选定的歌曲。 但是,在子视图控制器中有一个向后跳过并跳过前进button,所以当按下其中任何一个button并且再次显示父视图控制器时, 单元格文本现在需要突出显示与之前突出显示的单元格不同的单元格

所以我已经为每个视图控制器添加了适当的委托样板代码,但我不确定从子视图控制器获取的最佳信息以发回到父视图控制器,以及如何使用它。

子视图控制器

 -(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语句中添加一些内容,但是我不确定哪个最好

因为这是父视图控制器中的样子:

 - (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”这样的方法,它接受一些参数来表示按下button的types。

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

  3. 在子的indexChanged中,发送didSelectSkipButton消息给委托,传递表示按下的button的值。

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

在您的子视图控制器中创build委托

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

在您的子视图控制器中创build一个属性

 @property (nonatomic) id delegate; 

而当你按前进和后退button做这样的事情。

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

在父代中添加委托方法

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

 @property (assign) id <childDelegate> cDelegate; 

然后在父项中添加相同的方法

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

希望能帮助到你。