我有在其上6个按钮的视图控制器。这些按钮中的每一个都会推送一个表格视图控制器,该控制器将根据按钮具有的值进行传播。比方说,按钮是'汽车','面包车'等等,是否有可能在推送表格视图时记住按钮的值,以便可以基于由按钮交付的值,即#car,来搜索推文。我可以用6种不同的表格视图来完成这项工作,因为我可以根据搜索为每个方法分配viewDidLoad
方法,但我宁愿只做一次,并允许表格视图自动“填充”按钮上的值。这里是我的代码:按钮点击记住值时表视图被推
- (void)fetchTweets
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json"]];
NSError* error;
tweets = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return tweets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:@"text"];
NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];
return cell;
}
您可以在主控制器上创建6个用于推文的数组,您可以通过单击按钮来推送下一个视图,现在可以搜索该数组。 – rishi
感谢Rishi - 当表格视图被按下时,即当它从视图控制器移动到表格视图时,阵列是否会自动清空? – Alan
阵列将在您的主视图控制器弹出时清空,并且根据您的问题,主视图似乎不会在所需的场景中弹出。 – rishi