嗨我想重新加载我的表视图基于两个不同的数组。应该加载哪个数组由导航栏中的段控制决定。目前它只会加载第一个数组,当按下段控件时什么都不会发生。下面是我的代码任何帮助,为什么这是行不通的,非常感谢。我也检查过我的IBAction分段器连接在笔尖上。iOS重新加载UITableView使用UISegmentedControl
MessageViewController.h
#import <UIKit/UIKit.h>
@interface MessageViewController : UIViewController<UITableViewDelegate> {
IBOutlet UISegmentedControl *segmentControl;
IBOutlet UINavigationBar *navBar;
IBOutlet UITableView *tableView;
}
@property (retain, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *inbox;
@property (nonatomic, retain) NSMutableArray *sent;
@end
MessageViewController.m
#import "MessageViewController.h"
@interface MessageViewController() <UITableViewDelegate>
@end
@implementation MessageViewController
@synthesize segmentControl;
@synthesize inbox;
@synthesize sent;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title = NSLocalizedString(@"Messages", @"Messages");
self.tabBarItem.image = [UIImage imageNamed:@"mail_2_icon&32"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.inbox = [NSMutableArray arrayWithObjects:@"testing", @"test", @"another", nil];
self.sent = [NSMutableArray arrayWithObjects:@"test", @"another", @"testing", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(segmentControl.selectedSegmentIndex == 0){
return [inbox count];
}else if(segmentControl.selectedSegmentIndex == 1){
return [sent count];
}else{
return [inbox count];
}
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(segmentControl.selectedSegmentIndex == 0){
NSString *cellValue = [inbox objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}else if(segmentControl.selectedSegmentIndex == 1){
NSString *cellValue = [sent objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}else{
NSString *cellValue = [inbox objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
return cell;
}
-(IBAction)segmenter{
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
[inbox release];
[sent release];
[segmentControl release];
[segmentControl release];
[super dealloc];
}
- (void)viewDidUnload {
[self setSegmentControl:nil];
[segmentControl release];
segmentControl = nil;
[super viewDidUnload];
}
@end
在'segmenter'内设置断点。你看到程序执行该方法吗?如果是这样,你看到'self.segmentControl.selectedSegmentIndex'的不同值吗? – 2012-08-10 17:43:43
为什么当他们都在做同样的事情时,你需要'segmenter'中的开关?你也可以扔一些'NSLog'来看看程序在做什么。 – aforaudrey 2012-08-10 17:48:21
这是我的猜测 - 出口设置为酒吧按钮项目,而不是生活在其中的分段控制。在文档导航器中检查它实际连接到的对象。 – jrturton 2012-08-10 17:50:37