【问题标题】:Making this UITableviewCell clickable使这个 UITableviewCell 可点击
【发布时间】:2014-01-27 13:15:22
【问题描述】:

我目前正在学习本教程。 http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html。我已经完成了,一切正常。

我想要的是:您能告诉我如何使整个单元格可点击吗?网址应该是 JSON 提要中指定的网址。

这是.h:

#import "ViewController.h"

@interface ViewController ()
{
NSMutableArray *myObject;
// A dictionary object
NSDictionary *dictionary;
// Define keys
NSString *title;
NSString *thumbnail;
NSString *author;
NSString *permalink;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
title = @"title";
thumbnail = @"thumbnail";
author = @"author";
permalink = @"permalink";

myObject = [[NSMutableArray alloc] init];

NSData *jsonSource = [NSData dataWithContentsOfURL:
                      [NSURL URLWithString:@"http://ios-blog.co.uk/?feed=json"]];

id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers error:nil];

for (NSDictionary *dataDict in jsonObjects) {
    NSString *title_data = [dataDict objectForKey:@"title"];
    NSString *thumbnail_data = [dataDict objectForKey:@"thumbnail"];
    NSString *author_data = [dataDict objectForKey:@"author"];
    NSString *permalink_data = [dataDict objectForKey:@"permalink"];

    NSLog(@"TITLE: %@",title_data);
    NSLog(@"THUMBNAIL: %@",thumbnail_data);
    NSLog(@"AUTHOR: %@",author_data);
    NSLog(@"URL: %@",permalink_data);

    dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                  title_data, title,
                  thumbnail_data, thumbnail,
                  author_data,author,
                  permalink_data,permalink,
                  nil];
    [myObject addObject:dictionary];
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell=[[UITableViewCell alloc]initWithStyle:
    UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}


    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSMutableString *text;
    //text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:title]];
    text = [NSMutableString stringWithFormat:@"%@",
           [tmpDict objectForKeyedSubscript:title]];

    NSMutableString *detail;
    detail = [NSMutableString stringWithFormat:@"Author: %@ ",
             [tmpDict objectForKey:author]];

    NSMutableString *url;
    images = [NSMutableString stringWithFormat:@"%@ ",
             [tmpDict objectForKey:permalink]];



    cell.textLabel.text = text;
    cell.detailTextLabel.text= detail;

    return cell;
 }

谢谢。

【问题讨论】:

    标签: objective-c uitableview clickable


    【解决方案1】:

    单元格已经可以点击了,只需添加 -

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        //Do whatever you like with indexpath.row
    }
    

    希望您已将 tableview 的委托设置为 self。

    【讨论】:

    • 当我 NSlog 文本输出时,这很棒。但是,当我尝试这样做时: NSLog(@"URL: %@",permalink_data);它出现为构建失败:使用未声明的标识符。我如何让它将该 NSString 传递到我需要使用它的地方?
    • NSLog(@"URL: %@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]);
    • permalink_data 是字典的一部分,而字典又是 myobject 数组中的一个对象。
    【解决方案2】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"URL: %@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多