3

我正在使用故事板在 iOS 5 中创建我的第一个应用程序。我tableviewcontroller在导航控制器中嵌入了一个,当您单击有关该单元tableviewcontroller格主题的一些详细信息中的单元格时,应将 URL 传递给详细信息视图。我使用 aPList来填充表格视图。我未能将详细信息传递给详细信息视图...

这是我的代码(4个类):

//  NormalDataCell.m
#import "NormalDataCell.h"


@implementation NormalDataCell


@synthesize image = _image;
@synthesize nameLabel = _nameLabel;
@synthesize category = _category;
@synthesize webSiteURL = _webSiteURL;


@end

//  normalData.m
#import "NormalData.h"


@implementation NormalData


@synthesize name = _name;
@synthesize image = _image;
@synthesize webSiteURL = _webSiteURL;
@synthesize category = _category;


- (id)initWithItem:(NSDictionary *)item {

    self = [super init];
    if (self) {
        _name = [item objectForKey:@"name"];
        _image = [UIImage imageNamed:[item objectForKey:@"image"]];
        _webSiteURL = [item objectForKey:@"webSiteURL"];
        _category = [item objectForKey:@"category"];
    }
    return self;
}


@end


@implementation NormalCategory
@synthesize category = _category;
@synthesize normalList = _normalList;


- (id)initWithArray:(NSMutableArray *)normalList {

    self = [super init];
    if (self) {
        NSMutableArray *list = [[NSMutableArray alloc]init];
        for (NSDictionary *item in normalList) {
            NormalData *data = [[NormalData alloc]initWithItem:item];
            [list addObject:data];
        }

        NormalData *data = [list objectAtIndex:0];
        _category = data.category;
        _normalList = list;
    }
    return self;
}


@end

//  normalViewController.m

#import "NormalViewController.h"

#import "NormalData.h"

#import "NormalDataCell.h"

#import "NormalDetailView.h"



@interface NormalViewController ()

{

    @private

    NSMutableArray *_cellContentList;

}



@end



@implementation NormalViewController



@synthesize tableView; // Add this line of code

@synthesize roleArray;



- (void)viewDidLoad

{

    [super viewDidLoad];



    NSURL *url = [NSURL URLWithString:@"http://vfttx.web.fc2.com/normal.plist"];

    NSMutableArray *array = [NSMutableArray arrayWithContentsOfURL:url];



    _cellContentList = [[NSMutableArray alloc]init];

        for (NSMutableArray *item in array)

    {

        NormalCategory *category = [[NormalCategory alloc]initWithArray:item];

        [_cellContentList addObject:category];

    }

   }

//How do I pass the name & webSiteURL to DetailView?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"TableToWebSegue"]) {

    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSString *cellName = [[_cellContentList objectAtIndex:path.row] objectForKey:@"name"];

    [segue.destinationViewController setDetailItem2:cellName];

        NSString *urlPath = [[_cellContentList objectAtIndex:path.row] objectForKey:@"webSiteURL"];

        [segue.destinationViewController setWebSiteURL:urlPath];

}
}


#pragma mark - Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [_cellContentList count];

}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NormalCategory *category = [_cellContentList objectAtIndex:section];

    return [category category];

}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NormalCategory *category = [_cellContentList objectAtIndex:section];

    return [category.normalList count];

}



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

{

    static NSString *CellIdentifier = @"normalDataCell";

    NormalDataCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    // Configure the cell...

    if (cell == nil) {

        cell = [[NormalDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }



    NormalCategory *category = [_cellContentList objectAtIndex:[indexPath section]];

    NormalData *data = [category.normalList objectAtIndex:[indexPath row]];



    [cell.nameLabel setText:data.name];

    [cell.image setImage:data.image];

    cell.webSiteURL = data.webSiteURL;



    return cell;

}



@end

//  NormalDetailView.m
#import "NormalDetailView.h"


@interface NormalDetailView ()


@end


@implementation NormalDetailView


@synthesize webSiteURL;
@synthesize webView;
@synthesize name = _name;
@synthesize detailItem2;
@synthesize testNameLabel;


- (void)viewDidLoad
{
    [super viewDidLoad];

    testNameLabel.text = detailItem2;


//If the URL is right, it will work.
    NSString *strUrl = webSiteURL;
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}


@end
4

1 回答 1

2

大概你正在使用 segues 在不同的视图之间移动。

在包含您的表格视图的视图控制器上实现- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender,它将在用户点击单元格后被调用。它包含对 segue 对象的引用,您可以使用它来查找sourceViewControllerdestinationViewController,您可以使用它们来设置目标中的数据。

于 2012-10-28T18:25:42.270 回答