2014-01-26 68 views
1

我目前正在通過本教程。 http://thedarkdev.blogspot.co.uk/2013/09/web-service-apps-in-ios7-json-with.html。我已經完成了它,一切工作正常。使這個UITableviewCell可點擊

我想要的是:你能告訴我如何使整個單元格可點擊 - 該網址應該是在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; 
} 

感謝。

回答

5

細胞已經點擊,只需添加 -

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    //Do whatever you like with indexpath.row 
} 

我希望你已經設置的tableview的委託自我。

+0

這很棒,當我NSlog文本了。但是,當我嘗試做:NSLog(@「URL:%@」,permalink_data);它會在構建失敗時出現:使用未聲明的標識符。我如何獲得它將NSString傳遞到需要使用它的地方? – MarkP

+0

NSLog(@「URL:%@」,[[myObject objectAtIndex:indexPath.row] objectForKey:permalink]); –

+0

permalink_data是您的字典的一部分,它又是myobject數組中的一個對象。 –

2
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"URL: %@", [[myObject objectAtIndex:indexPath.row] objectForKey:permalink]); 
}