Question

I am trying to display the keywords searched in a Twitter app's search field in a table view, along with the number of tweets returned for this search. From the main ViewController, I'm trying to create another table view programmatically. And whenever I add an object to the NSMutableArray ( which is the data for the table that I want to display), the object gets stored in it but for the next function call the array doesn't retain its value. Since I'm using ARC, I cannot use the "retain" and "release" statements either.

How do I fix this problem?

Kindly please help.

@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
Was it helpful?

Solution

Put your [searchCount addObject:count]; code after you 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];
                 }
             }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top