单击TableView中的图像

时间:2015-03-10 13:36:56

标签: ios objective-c uitableview tableview

下午好,

我正在尝试通过在TableViewController(TableView)中单击配置文件图像来创建一个ProfileViewController(加载用户配置文件,如配置文件图像,图片和图片)。

目前无法正常工作,因为我触摸的所有东西都会进入Segue(帖子的segue,你可以看到条目,喜欢和评论)但我需要创建一些东西(也许是触摸手势)该作者的作者形象,并告诉我他的个人资料。

我尝试过创建一个Tap Gesture Recognizer,但它没有用。也是一个直接在故事板中的Segue行动,它不起作用。

我将向您展示一个例子,因为最终它就像Facebook墙一样:

Example Image

如果我触摸个人资料图片,则会显示包含其信息的用户个人资料页面。我希望实现这一点,但使用TableView中的图像。

这就是我现在所得到的:

TableViewController.m

//
//  CarTableViewController.m
//  Copyright (c) 2014. All rights reserved.
//

#import "CarTableViewController.h"
#import "CarTableViewCell.h"
#import "CarTableViewController.h"
#import "CarDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

@implementation CarTableViewController

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

@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 *)indexPath
{
    static NSString *CellIdentifier = @"carTableCell";

    CarTableViewCell *cell = [tableView
                              dequeueReusableCellWithIdentifier:CellIdentifier];

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

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

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

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

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

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

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

    return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    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)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://website.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];
            }
         NSLog(@"carModels ==> %@", _jsonArray);

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

        }
    );
}

@end

此致

提前致谢。

2 个答案:

答案 0 :(得分:1)

我曾经用手势识别器做到这一点......

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnCheck:)];

    tapped.numberOfTapsRequired = 1;
    tapped.numberOfTouchesRequired = 1;
    tapped.cancelsTouchesInView = YES;
    [cell.imageView addGestureRecognizer:tapped];

正如您所看到的,我的目标行动是&#34; tapOnCheck:&#34;我的图像是cell.imageView,但您可以简单地将此代码调整为您的代码...

希望这有帮助!

再见

答案 1 :(得分:0)

您可以尝试简单的解决方案。

从细胞中移除所有segues。 然后你需要覆盖CarTableViewCell,UIView的方法,该方法调用:   - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件; 在此方法中检测触摸位置。将其与图像的位置进行比较。 如果触摸位置在图像中 - 向单元格的委托(它是您的控制器)发送消息,表示触摸单元格中的图像。 否则 - 向小区的代表发送消息,该消息表示在Cell中的其他位置触摸。

然后选择segue推。

希望这会对你有所帮助。