在我的 TableViewController 中使用 TouchesBegan

Using TouchesBegan in my TableViewController

下午好,

我想使用 "TouchesBegan" 在我的表格ViewController 的一行中点击个人资料图片(如 Facebook 应用程序)以显示一个新的 ViewController,其中包含有关用户的更多信息.这意味着从 TableViewController(触摸 TableViewCell 的图像)然后转到(segue)另一个名为“ProfileViewController 的 ViewController ".

当我试图在我的 TableViewCell 中直接这样做时,它没有用,因为它不是 ViewController(它是 TableViewController 的子类) ) 所以我无法移动到另一个 ViewController。

所以,我现在要做的是在我的表 ViewController 的 UIImage 中创建一个 TouchesBegan,但我收到警告(然后通过崩溃)因为我使用 NSArray 来填充 UIImage URL 并且似乎我无法将 TouchesBegan 直接分配给图像。

你能帮我提供更多信息或一些例子吗?也许我缺少某些东西,因为这是我第一次尝试做这样的事情,我们将不胜感激。

这是我的代码:

那是我的桌子ViewController:

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

#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) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

    CGPoint pt = [[touches anyObject] locationInView:_profileImage];

    if (pt.x>=0 && pt.x<=100 && pt.y>=0 && pt.y<=100)
    {
        [self performSegueWithIdentifier:@"ID" sender:self];
    }
    else
    {
        NSLog(@"image not touched");
    }
}

/*
-(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

那是我的 TableViewController.h

//
//  CarTableViewController.h
//  TableViewStory
//

#import <UIKit/UIKit.h>

@interface CarTableViewController : UITableViewController

@property (nonatomic, strong) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *carImages;
@property (nonatomic, strong) NSMutableArray *carMakes;
@property (nonatomic, strong) NSMutableArray *carModels;

@property (nonatomic, strong) NSMutableArray *likes;
@property (nonatomic, strong) NSMutableArray *comments;
@property (nonatomic, strong) NSMutableArray *username;
@property (nonatomic, strong) NSMutableArray *refuser;
@property (nonatomic, strong) NSMutableArray *profileImage;

@property (nonatomic, strong) NSMutableArray *jsonArray;

@property (nonatomic, strong) IBOutlet UIImage *touchImageVIew;

@end

提前致谢。

如果您的 UIImageView 应该像按钮一样工作,您应该将它设为 UIButton 而不是 UIImageView。使用 UIButton,您可以在 Interface Builder 中设置一个 segue。这是 UITableViewController 的示例:

class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    let dataSource = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource.addObject(UIImage(named: "image.jpg")!)
        dataSource.addObject(UIImage(named: "image.jpg")!)

        tableView.reloadData()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("tableviewcell", forIndexPath: indexPath) as TableViewCell

        let image = dataSource.objectAtIndex(indexPath.row) as UIImage
        cell.button.setBackgroundImage(image, forState: UIControlState.Normal)

        return cell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "pushtodetail" {
            println("performing segue from a button in a cell")
        }
    }


}