嗨我想重新加載我的表視圖基於兩個不同的數組。應該加載哪個數組由導航欄中的段控制決定。目前它只會加載第一個數組,當按下段控件時什麼都不會發生。下面是我的代碼任何幫助,爲什麼這是行不通的,非常感謝。我也檢查過我的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