不能保留NSMutableArray对象(我正在使用ARC)
我试图显示在表格视图中的Twitter应用搜索字段中搜索的关键字以及为此搜索返回的推文数量。 从主ViewController,我试图以编程方式创建另一个表视图。每当我将一个对象添加到NSMutableArray(我想要显示的表的数据)时,该对象就会存储在其中,但对于下一个函数调用,该数组不会保留其值。由于我使用ARC,因此我无法使用“保留”和“释放”语句。不能保留NSMutableArray对象(我正在使用ARC)
我该如何解决这个问题?
请帮忙。
@interface AppViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) NSMutableArray *searchWords;
@property (strong,nonatomic) NSMutableArray *searchCount;
@property (strong,nonatomic) NSString *count;
@property (strong,nonatomic) UITableView *historyTable;
@property (strong, nonatomic) IBOutlet UITextField *searchTextField;
- (IBAction)showHistory:(id)sender;
@end
@implementation TwitterSearchViewController
@synthesize searchTextField;
@synthesize searchCount,searchWords,count,historyTable;
- (void)viewDidLoad
{
count=[[NSString alloc] init];
searchWords=[[NSMutableArray alloc] init];
searchCount=[[NSMutableArray alloc] init];
historyTable=[[UITableView alloc] init];
[super viewDidLoad];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"transitionToSearchResults"])
{
NSLog(@"Count of searchWords before:%lu",(unsigned long)[searchCount count]);
//This prints 0 every time
[searchWords addObject:[searchTextField text]];
NSLog(@"Count of searchWords:%lu",(unsigned long)[searchWords count]);
//This prints 1 every time
TWRequest *requestError = [[TWRequest alloc] initWithURL:[NSURL URLWithString:searchURL] parameters:nil requestMethod:TWRequestMethodGET];
//searchURL is used to get the tweets using Twitter API
// Send the Twitter Search request and create a handler to handle the Search response
[requestError performRequestWithHandler:^(NSData *searchResponse, NSHTTPURLResponse *requestResponse, NSError *error)
{
// Extract the Twitter messages from search response
NSArray *tweetMessages = [searchResultsDict objectForKey:@"results"];
// Set the Data Source for the Table in TwitterSearchResultsViewController
// which displays the search results nicely
[searchViewController setTweetMessages:tweetMessages];
// Update the table view
[searchViewController updateTableView];
count=[[NSString alloc] initWithFormat:@"%lu",(unsigned long)[tweetMessages count]];
}
}];
[searchCount addObject:count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [searchWords count];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *[email protected]"HistoryCell";
UITableViewCell *newCell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(newCell==nil){
newCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
}
[[newCell textLabel] setText:[searchWords objectAtIndex:[indexPath row]]];
[[newCell detailTextLabel] setText:[searchCount objectAtIndex:[indexPath row]]];
return newCell;
}
- (IBAction)showHistory:(id)sender {
//historyTable=[[UITableView alloc] init];
historyTable.dataSource=self;
historyTable.delegate=self;
NSLog(@"Histroy count:%ld",(long)[historyTable numberOfRowsInSection:0]);
//Since the mutable arrays are not retaining their values, the number of rows in my table is always zero
self.view=historyTable;
[email protected]"Search History";
}
@end
把你的[searchCount addObject:count];
代码在caliculated count之后。
[requestError performRequestWithHandler:^(NSData *searchResponse, NSHTTPURLResponse *requestResponse, NSError *error)
{
// Extract the Twitter messages from search response
NSArray *tweetMessages = [searchResultsDict objectForKey:@"results"];
// Set the Data Source for the Table in TwitterSearchResultsViewController
// which displays the search results nicely
[searchViewController setTweetMessages:tweetMessages];
// Update the table view
[searchViewController updateTableView];
count=[[NSString alloc] initWithFormat:@"%lu",(unsigned long)[tweetMessages count]];
[searchCount addObject:count];
}
}];
谢谢。我也想出了这个。但我改变了viewDidLoad中的代码来检查这些变量是否为零,如果是这样,那么我只是在分配和初始化它们。随着移动这一行代码完全解决了我的问题。 – user1646683 2013-03-15 04:13:26
好的..最热烈的:) – 2013-03-15 04:15:06
您正在prepareForSegue中添加对象。什么时候再次被调用?你会回到这个控制器吗?如果是的话,怎么样? – rdelmar 2013-03-14 23:29:34
我严重怀疑你的问题与你的数组没有保留其内容有任何关系。尝试把日志放在viewDidLoad中。当你前后往这个控制器时是否记录了多次? – rdelmar 2013-03-14 23:47:56
雅,我把一个日志消息在viewDidLoad,并帮助我解决了这个问题。由于每次切换回视图时都调用viewDidLoad方法,因此我的值已丢失。我现在修好了,谢谢! :) – user1646683 2013-03-15 04:09:56