2012-08-10 97 views
2

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

在'segmenter'內設置斷點。你看到程序執行該方法嗎?如果是這樣,你看到'self.segmentControl.selectedSegmentIndex'的不同值嗎? – 2012-08-10 17:43:43

+1

爲什麼當他們都在做同樣的事情時,你需要'segmenter'中的開關?你也可以扔一些'NSLog'來看看程序在做什麼。 – aforaudrey 2012-08-10 17:48:21

+0

這是我的猜測 - 出口設置爲酒吧按鈕項目,而不是生活在其中的分段控制。在文檔導航器中檢查它實際連接到的對象。 – jrturton 2012-08-10 17:50:37

回答

1

不知道爲什麼它不到底我所做的只是刪除三類和重做上面的東西代碼凡事要有剛剛一路上borked工作,但它的工作,現在和我一個快樂的露營者。不需要委託的東西,因爲這一切都在筆尖上完成,所以我原來的代碼工作正常。

0

我看到的唯一問題是,你使用的是與在它的表視圖中的視圖控制器,但你沒有在你的viewDidLoad中爲它設置代理,而且你沒有爲你的視圖控制器實現委託。

@interface MessageViewController() <UITableViewDelegate, UITableViewDataSource> 

@end 

viewDidLoad

self.tableView.delegate = self; 
self.tableView.dataSource = self; 

另外,還要確保你所擁有的一切在IB正確掛接,您的分段控制,你的表視圖兩種。

編輯:我做我自己的測試按照你想要做什麼,Here's my own implementation file

+0

謝謝你的回答我更新了我的代碼上面,但它仍然不工作 – GFlam 2012-08-10 18:02:27

+0

好吧,另一個建議,這是通常的良好做法。不要直接調用你的ivars(tableView.method),而是使用你定義的self.tableView.method的訪問器方法。同時擴展UITableViewDataSource並進行設置。 – 8vius 2012-08-10 18:19:10

+0

好吧,我做了我自己的測試,並使其工作。當您爲分段控件設置IBAction時,請確保它位於Value Changed事件中。 – 8vius 2012-08-10 19:04:40

0

設置委託和數據源的方法,並採取斷點來檢查數據。你的代碼是正確的。

tableview.delegate=self; 
tableView.datasource=self; 
+0

我通過將表視圖連接到文件所有者而在筆尖執行了此操作,但是我只是將其添加到了我的viewdidload中,但仍然沒有工作 – GFlam 2012-08-10 18:04:17

+0

if(segmentControl。selectedSegmentIndex == 0){ cell.textLabel.text = [inbox objectatIndex:IndexPath.row];嘗試一下... – Bajaj 2012-08-10 18:12:10