【问题标题】:How to load URL on UITableViewCell touch如何在 UITableViewCell 触摸上加载 URL
【发布时间】:2013-04-22 06:22:42
【问题描述】:

这是新的,只是在触摸某个 UITableView 单元格时尝试加载链接。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
  NSArray *deals = [dic objectForKey:@"deals"];
  NSDictionary *dealOne = [deals objectAtIndex:0];
  NSString *url = [dealOne objectForKey:@"url"];

  UITouch *touch = [[event allTouches] anyObject];

  if([touch isKindOfClass:[UITableViewCell class]])
  {
    UITableViewCell *cell;
    CGPoint location = [touch locationInView: self.view];
    cell.center = location;

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
  }
  NSLog(@"TABLECELL TOUCHED");
}

【问题讨论】:

  • 为什么你使用 touchesBegan,UITableView 已经有一个触摸检测委托,它是 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。在这个方法中你可以简单地调用[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

标签: arrays uitableview xcode4


【解决方案1】:

使用以下方法解决。在这种情况下,我希望在为“finalDealOne”触摸单元格时加载链接。双击单元格时,将加载链接。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
  NSArray *deals = [dic objectForKey:@"deals"];
  NSDictionary *dealOne = [deals objectAtIndex:0];
  NSString *title = [dealOne objectForKey:@"title"];
  NSString *finalDealOne = [NSString stringWithFormat:@"Deal One:\n\n''%@''\n\nDouble tap here to redeem offer.",title];

  (NSIndexPath *)indexPath
  UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(finalDealOne)];
  tapped.numberOfTapsRequired = 2;

  [cell addGestureRecognizer:tapped];
}

为了完成这项工作,我提供了以下参考:

 - (void)finalDealOne
 { 
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
    NSArray *deals = [dic objectForKey:@"deals"];
    NSDictionary *dealOne = [deals objectAtIndex:0];
    NSString *url = [dealOne objectForKey:@"url"];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多