在TableViewController中使用TapGestureRecognizer和profileimage

时间:2015-04-21 12:45:11

标签: ios objective-c uitableview tableview uitapgesturerecognizer

下午好,

我正在尝试实现一个“查看用户配置文件”按钮,该按钮将显示带有用户信息的新ViewController。我在按下图像时在图像配置文件上实现了TapGestureRecognizer(它是一个TableViewController)。

主要问题是,当我按下用户图像时,它显示一个错误的ProfileViewController,因为当我按下另一行时,我的TableViewController似乎没有更新@username(它显示了我见过的最新配置文件)。

我尝试过很多教程,但它在任何情况下都无法正常工作,因为每当我按下配置文件图像时,它都显示错误的用户名(我已经测试过了)。

要将用户名更新为我按下的用户名,我需要做些什么?

查找我的XCode的图片:http://i.stack.imgur.com/FvNHg.jpg

这是我的 TableViewController.m (它位于@“segueprofile”)

//
//  CarTableViewController.m
//  TableViewStory
//

#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import "OtherProfileUserViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <Security/Security.h>
#import "SSKeychainQuery.h"
#import "SSKeychain.h"
#import "SBJson.h"

@interface NSURLRequest (DummyInterface)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;

@end

@interface CarTableViewController ()

@end

@implementation CarTableViewController

@synthesize carMakes = _carMakes;
@synthesize carModels = _carModels;
@synthesize carImages = _carImages;

@synthesize like = _like;
@synthesize likes = _likes;
@synthesize comments = _comments;
@synthesize username = _username;
@synthesize refuser = _refuser;
@synthesize profileImage = _profileImage;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self fetchJson];

    [self.tableView reloadData];

    // Initialize the refresh control.
    self.refreshControl = [[UIRefreshControl alloc] init];
    //self.refreshControl.backgroundColor = [UIColor blackColor];
    //self.refreshControl.tintColor = [UIColor whiteColor];
    [self.refreshControl addTarget:self
                            action:@selector(fetchJson)
                  forControlEvents:UIControlEventValueChanged];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_jsonArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)anIndexPath
{
    static NSString *CellIdentifier = @"carTableCell";

    CarTableViewCell *cell = [tableView
                              dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CarTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }

    cell.likeButton.tag = anIndexPath.row;

    // Configure the cell...
    cell.makeLabel.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"id"];
    cell.likes.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"likes"];
    cell.comments.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"comments"];
    cell.username.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"username"];
    cell.refuser.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"user_ref"];

    cell.modelLabel.text = [[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"user"];

    NSURL * imageURL = [NSURL URLWithString:[[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"imagen"]];

    [cell.carImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"imagen"] options:SDWebImageRefreshCached];

    NSURL * imageURL2 = [NSURL URLWithString:[[_jsonArray objectAtIndex:anIndexPath.row] valueForKey:@"image"]];

    [cell.profileImage setImageWithURL:imageURL2
                      placeholderImage:[UIImage imageNamed:@"image"]
                            options:SDWebImageRefreshCached];

    return cell;
}

-(void)fetchJson {

    self.carModels = [[NSMutableArray alloc] init];
    self.carMakes = [[NSMutableArray alloc] init];
    self.carImages = [[NSMutableArray alloc] init];
    self.likes = [[NSMutableArray alloc] init];
    self.comments = [[NSMutableArray alloc] init];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{

        NSString * urlString = [NSString stringWithFormat:@"http://webiste.com/service.php"];
        NSURL * url = [NSURL URLWithString:urlString];
        NSData * data = [NSData dataWithContentsOfURL:url];

        NSError *error;
        [_jsonArray removeAllObjects];
        _jsonArray = [NSJSONSerialization
                      JSONObjectWithData:data
                      options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
                      error:&error];

            for(int i=0;i<_jsonArray.count;i++)
            {
                NSDictionary * jsonObject = [_jsonArray objectAtIndex:i];
                NSString* imagen = [jsonObject objectForKey:@"imagen"];
                [_carImages addObject:imagen];

                NSDictionary * jsonObject2 = [_jsonArray objectAtIndex:i];
                NSString* user = [jsonObject2 objectForKey:@"user"];
                [_carMakes addObject:user];

                NSDictionary * jsonObject3 = [_jsonArray objectAtIndex:i];
                NSString* date = [jsonObject3 objectForKey:@"date"];
                [_carModels addObject:date];
            }

        dispatch_async(dispatch_get_main_queue(), ^{
            {
                [self.tableView reloadData];
                [self.refreshControl endRefreshing];
            }});

        }
    );

    //NSLog(@"LINK ==> %@", _jsonArray);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    //NSIndexPath *indexPath = [self tableView indexPathForCell:sender];

    //NSLog(@"LINK ==> %@", indexPath);

    if ([segue.identifier isEqualToString:@"segueprofile"]) {
        OtherProfileUserViewController *destViewController = segue.destinationViewController;
        destViewController.recipeName = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"username"];
    }
    if ([segue.identifier isEqualToString:@"segueprofile2"]) {
        OtherProfileUserViewController *destViewController = segue.destinationViewController;
        destViewController.recipeName = [[_jsonArray objectAtIndex:indexPath.row] valueForKey:@"username"];
    }
    if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
    {
        CarDetailViewController *detailViewController = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

        detailViewController.carDetailModel = [[NSArray alloc]
                                               initWithObjects:
                                               [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"date"],
                                               [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"id"],
                                               [[_jsonArray objectAtIndex:[myIndexPath row]] valueForKey:@"imagen"],
                                               nil];
    }
}

- (void) alertStatus:(NSString *)msg :(NSString *) title
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alertView show];
}

- (IBAction)plusOne:(UIButton *)sender {

    NSLog(@"%ld",(long)sender.tag);
}

@end

非常感谢任何帮助。

此致

0 个答案:

没有答案