如何通过NSMutableArray在UITableView上显示数据?

时间:2011-11-27 14:07:13

标签: objective-c uitableview nsmutablearray

我尝试使用NSMutableArray对象作为数据容器来存储数据。

@property (retain, nonatomic) NSMutableArray* allTweets;

我从网络中获取JSON并将其解析为allTweets。但是我无法重新加载数据 [self.tableView reloadData];我在SIGABRT

上收到main.m

我尝试在属性中使用strong。它又崩溃了。当我使用weak时,不再发生崩溃。但是allTweets总是没有。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //* other codes *//
        self.allTweets = [[NSMutableArray alloc]initWithCapacity:50];
        [self getTimeline:nil];
    }

-(void) parseTweetJSON:(NSData*) responseData
{
    NSMutableArray* tempTweets = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
   //convert each element in array into nsdictionary
    for (NSDictionary *aTweet in tempTweets)
    {
        NSLog(@"%@ (by @%@)", [aTweet objectForKey:@"text"], [[aTweet objectForKey:@"user"] objectForKey:@"screen_name"]);
        Tweet* theTweet = [[Tweet alloc]init];    

        theTweet.text = [aTweet valueForKey:@"text"];
        theTweet.screenName = [[aTweet valueForKey:@"user"] valueForKey:@"screen_name"];
        theTweet.createAt = [aTweet valueForKey:@"create_at"];

        NSLog(@"class is %@", [[aTweet valueForKey:@"text"]class]);
        [allTweets addObject:theTweet];
    }

    if([allTweets count]>0)
    {
        [self.tableView reloadData];
    }

}

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

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.textLabel.numberOfLines = 0;
        cell.detailTextLabel.numberOfLines = 0;
    }

    NSInteger row = [indexPath row];
    Tweet* aTweet = [[Tweet alloc]init];
    aTweet = [allTweets objectAtIndex:row];

    cell.textLabel.text = aTweet.screenName;
    cell.detailTextLabel.text = aTweet.text;

    return cell;
}

我应该使用哪种属性类型NSMutableArray,保留,复制,强。 顺便说一句,我使用Xcode 4.2

  

2011-11-28 02:54:23.968 SparkTweet [2039:10703]类是__NSCFString
  2011-11-28 02:54:23.969 SparkTweet [2039:10703] - [鸣叫
  isEqualToString:]:无法识别的选择器发送到实例0x6e46c50
  2011-11-28 02:54:23.970 SparkTweet [2039:10703] *终止应用程序到期   未被捕获的异常' NSInvalidArgumentException',原因:' - [Tweet
  isEqualToString:]:无法识别的选择器发送到实例0x6e46c50'
  *
第一次抛出调用堆栈:(0x14c1052 0x189dd0a 0x14c2ced 0x1427f00 0x1427ce2 0x1b168f 0x4edf 0x24e548 0x250722 0x1007c7 0x1002c1 0x4371
  0x14c2e72 0x98b9ef 0x149597f 0x13f8c39 0x13f8454 0x13f7db4 0x13f7ccb
  0x1f8f879 0x1f8f93e 0x73a9b 0x2bf8 0x2b55 0x1)终止调用
  抛出异常

reloadData引发的异常。 NSLog最近发布了20条推文。

这是我的推文类声明

#import <Foundation/Foundation.h>

@interface Tweet : NSObject
{
    NSString* name;
    NSString* screenName;
    NSString* text;
    NSString* createAt;
}

@property (copy,nonatomic) NSString* name;
@property (copy,nonatomic) NSString* screenName;
@property (copy,nonatomic) NSString* text;
@property (copy,nonatomic) NSString* createAt;

@end

1 个答案:

答案 0 :(得分:0)

  1. 最重要的不是您为allTweets数组使用的属性属性,而是如何定义Tweet的属性。如果它们不是retain,则会崩溃。

  2. 这是什么?

    Tweet * aTweet = [[Tweet alloc] init];
    a aTweet = [allTweets objectAtIndex:row];

  3. 您应该将autorelease添加到cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];中的cellForRowAtIndexPath:Tweet* theTweet = [[Tweet alloc]init]中的parseTweetJSON: