在 Actionsheet 上选择的 NSDictionary,来自带有自定义单元格的 UITableView

Posted

技术标签:

【中文标题】在 Actionsheet 上选择的 NSDictionary,来自带有自定义单元格的 UITableView【英文标题】:NSDictionary selected on Actionsheet, from a UITableView with Custom Cells 【发布时间】:2011-03-08 16:13:16 【问题描述】:

我正在研究一个重要的学习经验,添加一个使用 JSON(反序列化器)从我的 MAMP(myphp 管理员)服务器(localhost)获取内容的 NSDictionary,并将所有这些信息很好地放在 TablewView 中,它具有自定义UITableViewCell 的,

我的问题是,表视图可以很好地从我的服务器加载信息,但是,当我使用 didSelectRowatIndexPath 和操作表时,我可以启动操作表,但是我无法从中提取信息我以前在 UITableView 中使用的 NSDictionary ......(基本上最后,操作表将有几个按钮,例如从共享应用程序加载 URL,所有这些信息都将使用从我的服务器获取UITable 视图中的相同 NSDictionary。

任何帮助将不胜感激......非常感谢这个社区摇滚!!!

//
//  TouchJSONViewController.m
//  TouchJSON
//
//  Created by Chance Brown on 3/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TouchJSONViewController.h"
#import "CJSONDeserializer.h"
#import "StaffPicksCustomCell.h"
#import "UIImageView+WebCache.h"

@implementation TouchJSONViewController

@synthesize tableview, rows, cellOne;

- (void)viewDidLoad 
    [super viewDidLoad];    
    NSURL *url = [NSURL URLWithString:@"http://localhost/json.php"]; // Modify this to match your url.
    // HOME 192.168.2.23    
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
//  NSLog(jsonreturn); // Look at the console and you can see what the restults are 
    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    NSError *error = nil;   
    // In "real" code you should surround this with try and catch
    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    
        rows = [[dict objectForKey:@"users"] retain];
    
    NSLog(@"Array: %@",rows);   
    [jsonreturn release];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return [rows count];

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     
    static NSString *CellIdentifier = @"Cell";
    StaffPicksCustomCell *cell =(StaffPicksCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[StaffPicksCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    

    // Configure the cell.  
    NSSortDescriptor *ratingsSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"ID" ascending:NO] autorelease];
    rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];

    NSDictionary *dict = [rows objectAtIndex: indexPath.row];

    cell.primaryLabel.text = [dict objectForKey:@"post_title"];
    cell.theDate.text = [dict objectForKey:@"post_date"];
    cell.mydescription.text = [dict objectForKey:@"post_content"];  

    [cell.myImageView setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"imagelink"]]
                     placeholderImage:[UIImage imageNamed:@"placeholder1.png"]];


    //cell.textLabel.text = [dict objectForKey:@"post_title"];
    //cell.detailTextLabel.text = [dict objectForKey:@"post_content"];  
    //tableView.backgroundColor = [UIColor cyanColor];  
    return cell;


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    return 125;


#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    static NSString *CellIdentifier = @"Cell";
    StaffPicksCustomCell *cell = (StaffPicksCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StaffPicksCustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) 
            if ([currentObject isKindOfClass:[StaffPicksCustomCell class]]) 
                cell= (StaffPicksCustomCell *) currentObject;
                break;
            
        
       
    // Configure  - Did Select Row at Index Path.
    UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"OPTIONS" delegate:self cancelButtonTitle:@"Cancel"destructiveButtonTitle:nil otherButtonTitles:@"More Info",@"Contact Seller",@"Picture", nil];   
    [popup setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    [popup showInView:[self view]];
    [popup release];
    // return cell  


-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)indexPath


    switch (indexPath) 
        case 0:
        
            NSLog(@"Case 0 Selected");
            NSDictionary *dict = [rows objectAtIndex: indexPath];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[dict objectForKey:@"guid"]]];
        

            break;

        case 1: 
        
        NSLog(@"Case 1 Selected");  
        
            break;  
        case 2:
        
        NSLog(@"Case 2 Selected");  
        
            break;
    



- (void)didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


- (void)viewDidUnload 
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;


- (void)dealloc 
    [super dealloc];


@end

【问题讨论】:

【参考方案1】:

这是行,它们正在被自动释放。这是重要的几行。

viewDidLoad 中的第一行正确地保留了行

rows = [[dict objectForKey:@"users"] retain];

但是您在 cellForRowAtIndexPath 中对这些行进行排序并没有保留它。

rows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];

这应该是这样的:

NSArray *sortedRows = [rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]];
[rows release];
rows = [sortedRows retain];

希望这会有所帮助。

【讨论】:

该代码确实有帮助,但似乎是 []retain] 完美地解决了这个问题!非常感谢
 NSSortDescriptor *ratingsSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"ID" 升序:NO] 保留]; // [ 保留 ] 添加到上面的行以记住信息。 rows = [[rows sortedArrayUsingDescriptors:[NSArray arrayWithObject:ratingsSortDescriptor]]retain]; // [ 保留 ] 添加到上面的行以记住信息。 

以上是关于在 Actionsheet 上选择的 NSDictionary,来自带有自定义单元格的 UITableView的主要内容,如果未能解决你的问题,请参考以下文章

根据在第一个选项上选择的选项值隐藏选项选择值

ngModel 显示在 md-select 上选择的先前值

根据datepicker上选择的日期禁用整周

如何将集合视图上选择的所有单元格列出到另一个视图中

如何增加CalendarDatePicker上选择的日期的字体大小

Unity判断平台