无法保留NSMutableArray对象(我正在使用ARC)

时间:2013-03-14 23:22:16

标签: ios objective-c nsmutablearray automatic-ref-counting retain

我正在尝试在表格视图中显示在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

1 个答案:

答案 0 :(得分:0)

在计算计数后放置[searchCount addObject: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];
                 }
             }];