我試圖顯示在表格視圖中的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
您正在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