我试图在表格视图中显示在 Twitter 应用程序的搜索字段中搜索的关键字,以及为此搜索返回的推文数量。从主 ViewController,我试图以编程方式创建另一个表视图。并且每当我将一个对象添加到 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 *cellIdentifier=@"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;
self.title=@"Search History";
}
@end