如何通过 NSMutableArray 在 UITableView 上显示数据?
我尝试使用 NSMutableArray 对象作为数据容器来存储数据。
@property (retain, nonatomic) NSMutableArray* allTweets;
我从网络中获取 JSON 并将其解析为 allTweets
。但是我无法重新加载数据 [self.tableView reloadData];
我在 main.m
上收到 SIGABRT
我尝试在属性中使用 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
、retain、copy、strong 使用什么属性类型。 顺便说一句,我使用 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
I try to use a NSMutableArray
object as data container to store the data.
@property (retain, nonatomic) NSMutableArray* allTweets;
I grab the JSON from networking and parse it into allTweets
. However I cannot reloadData with[self.tableView reloadData];
I receive SIGABRT
on main.m
I try use strong
in property. It crash again. When I use weak
, no more crash happen. But the allTweets is always nil.
- (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;
}
What property type should I use for NSMutableArray
,retain,copy,strong.
BTW I use Xcode 4.2
2011-11-28 02:54:23.968 SparkTweet[2039:10703] class is __NSCFString
2011-11-28 02:54:23.969 SparkTweet[2039:10703] -[Tweet
isEqualToString:]: unrecognized selector sent to instance 0x6e46c50
2011-11-28 02:54:23.970 SparkTweet[2039:10703] * Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason: '-[Tweet
isEqualToString:]: unrecognized selector sent to instance 0x6e46c50'
* First throw call stack: (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) terminate called
throwing an exception
This exception thrown at reloadData
. The NSLog print 20 recent tweets.
here is my tweet class declare
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最重要的不是为 allTweets 数组使用哪些属性,而是如何定义 Tweet 的属性。如果它们不
retain
,它将崩溃。这是什么?
Tweet* aTweet = [[Tweet alloc]init];
aTweet = [allTweets objectAtIndex:row];
您应该将
autorelease
添加到cellForRowAtIndexPath:
中的cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
和 < code>Tweet* theTweet = [[Tweet alloc]init] inparseTweetJSON:
Most important is not what property attributes you use for allTweets array but how properties of Tweet are defined. If they are not
retain
, it will crash.What's this?
Tweet* aTweet = [[Tweet alloc]init];
aTweet = [allTweets objectAtIndex:row];
You should add
autorelease
tocell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
incellForRowAtIndexPath:
and toTweet* theTweet = [[Tweet alloc]init]
inparseTweetJSON: