如何将UIImageView从UITableView(自定义单元格)传递给DetailViewController?

时间:2014-08-23 15:56:31

标签: ios objective-c uitableview

我把头发拉过这个头发。我试图将图片网址从我的TableViewController传递到我的DetailViewControllerFullArticleViewController),以便我可以设置UIImageView,我尝试的任何内容似乎都没有工作。请参阅下面的代码:

MyTableViewController.h

@interface MyTableViewController : UIViewController  <UISearchBarDelegate>{

    IBOutlet UITableView *DoctorsTableView;

    NSArray *Doctors;
    NSMutableData *data;

    NSArray *searchResults;

}

@property (strong, nonatomic) NSString *cellImageLink;
@property (strong, nonatomic) UINavigationBar *navigationBar;

MyTableViewController.m

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

            static NSString *DoctorsTableIdentifier = @"DoctorsCell";

            DoctorsCell *cell = (DoctorsCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
            if (cell == nil) 
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DoctorsCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            } 

            if (tableView == self.searchDisplayController.searchResultsTableView) {

                NSLog(@"Using the search results");

            cell.firstnameLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"node_title"];

            cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Opening Paragraph"];

            NSString *firstLink = [[NSString alloc] init];

            firstLink = [[[searchResults objectAtIndex:indexPath.row] objectForKey:@"Image"] objectForKey:@"filename"];

            NSString *secondLink = [[NSString alloc] init];

            secondLink = [NSString stringWithFormat:@"URL HERE%@",firstLink];
              NSLog(@"second link is %@", secondLink);


            cellImageLink = secondLink;

            [cell.featureImage sd_setImageWithURL:[NSURL URLWithString:secondLink]];


            } else {
                NSLog(@"Using the Full List!");


                cell.firstnameLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];

                cell.descriptionLabel.text = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"Opening Paragraph"];

                NSString *firstLink = [[NSString alloc] init];

                firstLink = [[[Doctors objectAtIndex:indexPath.row] objectForKey:@"Image"] objectForKey:@"filename"];

                NSString *secondLink = [[NSString alloc] init];

                secondLink = [NSString stringWithFormat:@"URL HERE%@",firstLink];
                NSLog(@"second link is %@", secondLink);


                cellImageLink = secondLink;

                [cell.featureImage sd_setImageWithURL:[NSURL URLWithString:secondLink]];

            }

            return cell;
        }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        FullArticleViewController *detailViewController = [[FullArticleViewController alloc]
                                                            initWithNibName:@"FullArticleViewController" bundle:nil];
        if ([searchResults count]) {

            detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"node_title"];

            detailViewController.articleDetail = [searchResults objectAtIndex:indexPath.row];


        } else {

            detailViewController.title = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];

            detailViewController.articleDetail = [Doctors objectAtIndex:indexPath.row];

            NSLog(@"%@", Doctors);

        }

FullArticleViewController *viewController = [[FullArticleViewController alloc]
                                                     initWithNibName:@"DetailViewController"
                                                     bundle:nil];
 viewController.featureImage = searchResults[indexPath.row][@"Image"][@"filename"];
                                                                 [self.navigationController pushViewController:detailViewController animated:YES];
    }

FullArticleViewController.h (详情)

@interface FullArticleViewController : UIViewController

{

    IBOutlet UIScrollView *scroller;
    IBOutlet UILabel *firstnameLabel;
    IBOutlet UILabel *descriptionLabel;
    IBOutlet UILabel *bodyLabel;

}

@property (nonatomic, copy) NSDictionary *articleDetail;
@property (strong, nonatomic) IBOutlet UIImageView *featureImage;

-(IBAction)goBack:(id)sender;

FullArticleViewController.m (详情视图)

 #import "SDWebImage/UIImageView+WebCache.h"
    #import "FullArticleViewController.h"
    #import "DoctorsCell.h"
    #import "MyTableViewController.h"


    @interface FullArticleViewController ()

    @end

    @implementation FullArticleViewController

    @synthesize articleDetail;
    @synthesize featureImage;


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization

        }
        return self;
    }

    - (void)viewDidLoad
    {

featureImage = [[UIImageView alloc] init];

        [scroller setScrollEnabled:YES];
        [scroller setContentSize:CGSizeMake(320, 5000)];

        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

        firstnameLabel.text = [articleDetail objectForKey:@"node_title"];
        descriptionLabel.text = [articleDetail objectForKey:@"Opening Paragraph"];
        bodyLabel.text = [articleDetail objectForKey:@"Body"];

    }

2 个答案:

答案 0 :(得分:0)

<强> MyTableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    FullArticleViewController *detailViewController = [[FullArticleViewController alloc]
                                    initWithNibName:@"FullArticleViewController" bundle:nil];
    if ([searchResults count]) {
        detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"node_title"];
        detailViewController.articleDetail = [searchResults objectAtIndex:indexPath.row];
        detailViewController.cellImageLink = searchResults[indexPath.row][@"Image"][@"filename"];
    } else {
        detailViewController.title = [[Doctors objectAtIndex:indexPath.row] objectForKey:@"node_title"];
        detailViewController.articleDetail = [Doctors objectAtIndex:indexPath.row];
        detailViewController.cellImageLink =  Doctors[indexPath.row][@"Image"][@"filename"];
    }
        NSLog(@"cellImageLink = %@", detailViewController.cellImageLink);
        [self.navigationController pushViewController:detailViewController animated:YES]; 
}

FullArticleViewController.m

viewDidLoad

中设置图像
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"cellImageLink = %@", self.cellImageLink);

    featureImage = [[UIImageView alloc] init];
    //maybe set the frame here?
    [self.featureImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://domainnamehere.com/%@",cellImageLink]]];

    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 5000)];

    firstnameLabel.text = [articleDetail objectForKey:@"node_title"];
    descriptionLabel.text = [articleDetail objectForKey:@"Opening Paragraph"];
    bodyLabel.text = [articleDetail objectForKey:@"Body"];
}

答案 1 :(得分:0)

通常,在按下新视图控制器之前,您将在单元格选择时设置详细视图属性。像这样的东西会这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  FullArticleViewController *detailView = [[FullArticleViewController alloc] init];
  detailView.articleDetail = [searchResults objectAtIndex:indexPath.row];
  [self.navigationController pushViewController detailView animated:YES];
}

注意我正在使用视图控制器的默认初始值设定项,我还假设您使用的是UINavigationController。您可能需要更改其中一个或两个以适合您的代码。

然后,您希望使用相同的SDWebImage类别在viewDidLoad的{​​{1}}中设置图片网址。它可能看起来像这样:

FullArticleViewController.m