如何在UITableView和ViewController之间传递数据?

时间:2013-01-08 12:19:27

标签: objective-c ios

我必须在第二个场景中播放第一个场景UITableView,第二个场景是ViewController我有流媒体播放器。当有人在cell中选择UITableView时,我想存储单元格的名称,并且我想知道上次选择了哪个单元格。

如果需要,我可以显示我的代码。

   #import "MasterViewController.h"
#import "DetailViewController.h"

@class DetailViewController;
@interface MasterViewController () {
    NSMutableArray *_objects;
    NSMutableData* songData;
    NSURLConnection* connection;
    NSMutableArray* songsArray;
    NSMutableArray* songsURL;

}
@end

@interface MasterViewController()
    @property DetailViewController* detailsViewController;
    @property (nonatomic, strong) NSString* lastSelectedSong;
@end

@implementation MasterViewController

@synthesize currentSelectionTitle, sections, detailsViewController;
@synthesize lastSelectedSong = _lastSelectedSong;


- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Songs List";
    songsArray = [[NSMutableArray alloc]init];
    songsURL = [[NSMutableArray alloc] init];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://0.0.0.0/harrytest/WebservicesBackup/Copy%20of%20search.php?q=&opset=numeric&pagesize=numeric&find=Find"]];
    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        songData = [[NSMutableData alloc]init];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - JSON Data

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [songData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [songData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Failed with Error");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSDictionary* allSongsData = [NSJSONSerialization JSONObjectWithData:songData options:0 error:nil];
    NSArray* songs = [allSongsData objectForKey:@"songs"];

    for (NSDictionary* diction in songs) {
        NSDictionary* songsDic = [diction objectForKey:@"Song_Name"];
        [songsArray addObject:songsDic];
    }

    for (NSDictionary* diction in songs) {
        NSDictionary* songsDic = [diction objectForKey:@"url"];
        [songsURL addObject:songsDic];
    }

    [self.tableView reloadData];
}


#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger sectionCount = 1;
    return sectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return songsArray.count;
}

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

    cell.textLabel.text = [songsArray objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSString* currentSongSelect = [songsArray objectAtIndex:[indexPath row]];

    if (currentSongSelect == _lastSelectedSong) {
        NSLog(@"");
    }
    else {
        NSLog(@"");
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];

        NSString* currentSongName = [songsURL objectAtIndex:[indexPath row]];
        DetailViewController* detailViewController = [segue destinationViewController];
        [detailViewController setDetailItem:currentSongName];

        NSString* currentSongValue = [songsArray objectAtIndex:[indexPath row]];
        [detailViewController setItemValue:currentSongValue];

    }
}

-(void) passNameBack:(DetailViewController *)controller didPlayedSong:(NSString *)song {
    _lastSelectedSong = song;
    NSLog(@"LAST SONG:%@", _lastSelectedSong);
}


@end

2 个答案:

答案 0 :(得分:0)

如果您想知道单击表视图中的哪个单元格,请使用tableView:(UITableView*) didSelect..函数,该函数将告诉您选择了哪一行,以及是否要将其保存以供进一步使用,然后UITableViewCell * cell = [your_selected_cell retained]

答案 1 :(得分:0)

您可以通过Appdelegate传递值。

Appdelegate声明两个字符串,例如:

NSString *cellname;
NSString* currentSongName;

使用DidSelectRowAtindexpath下面的代码来节省价值。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 NSString* currentSongSelect = [songsArray objectAtIndex:[indexPath row]];

 AppDelegate *localVar = (AppDelegate *)[[UIApplication sharedApplication] delegate];

localVar.cellname=[songsArray objectAtIndex:[indexPath row]];
localVar.rownumber=indexpath.row;
localVar.currentSongName = [songsURL objectAtIndex:[indexPath row]];

if (currentSongSelect == _lastSelectedSong) {
        NSLog(@"");
    }
    else {
        NSLog(@"");
    }
}

要在detailsviewController中检索数据,请使用以下代码

AppDelegate *localVar = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *cellname=localVar.cellname;
NSinteger rownumber=localVar.rownumber;
NSString* currentSongName=localVar.currentSongName;