使用UISegmentedControl重新加载iOS UITableView

使用UISegmentedControl重新加载iOS UITableView,ios,uitableview,uisegmentedcontrol,Ios,Uitableview,Uisegmentedcontrol,嗨,我正在尝试根据两个不同的数组重新加载我的表视图。应该加载哪个数组由导航栏中的段控件决定。目前它只加载第一个数组,当按下段控件时不会发生任何事情。下面是我的代码。如果您能帮我解释为什么这样做不起作用,我将不胜感激。我还检查了我的IBAction分段器是否连接在nib中 MessageViewController.h #import <UIKit/UIKit.h> @interface MessageViewController : UIViewController<UITa

嗨,我正在尝试根据两个不同的数组重新加载我的表视图。应该加载哪个数组由导航栏中的段控件决定。目前它只加载第一个数组,当按下段控件时不会发生任何事情。下面是我的代码。如果您能帮我解释为什么这样做不起作用,我将不胜感激。我还检查了我的IBAction分段器是否连接在nib中

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
#导入
@接口消息ViewController:UIViewController{
IBUI分段控制*分段控制;
IBUINAVIGATIONBAR*导航栏;
IBUITableView*表格视图;
}
@属性(保留,非原子)IBUI分段控件*分段控件;
@属性(保留,非原子)IBUITableView*tableView;
@属性(非原子,保留)NSMutableArray*收件箱;
@属性(非原子,保留)NSMutableArray*已发送;
@结束
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
#导入“MessageViewController.h”
@接口消息ViewController()
@结束
@实现消息视图控制器
@综合分段控制;
@综合收件箱;
@综合发送;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
self.tabBarItem.title=NSLocalizedString(@“Messages”,@“Messages”);
self.tabBarItem.image=[UIImage ImageName:@“邮件图标&32”];
}
回归自我;
}
-(无效)viewDidLoad{
[超级视图下载];
self.tableView.delegate=self;
self.inbox=[NSMutableArray数组,其对象:@“testing”、@“test”、@“other”、nil];
self.sent=[NSMutableArray数组,其对象为:@“test”,“other”,“testing”,nil];
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
#pragma标记表视图方法
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
如果(segmentControl.selectedSegmentIndex==0){
返回[收件箱计数];
}else if(segmentControl.selectedSegmentIndex==1){
返回[已发送计数];
}否则{
返回[收件箱计数];
}
}
-(UITableViewCell*)tableView:(UITableView*)aTableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[aTableView出列重用CellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
如果(segmentControl.selectedSegmentIndex==0){
NSString*cellValue=[inbox objectAtIndex:indexath.row];
cell.textlab.text=cellValue;
}else if(segmentControl.selectedSegmentIndex==1){
NSString*cellValue=[sent objectAtIndex:indexPath.row];
cell.textlab.text=cellValue;
}否则{
NSString*cellValue=[inbox objectAtIndex:indexath.row];
cell.textlab.text=cellValue;
}
返回单元;
}
-(IBAction)分段器{
[tableView重新加载数据];
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
}
-(无效)解除锁定{
[收件箱发布];
[发送释放];
[控制释放];
[控制释放];
[super dealoc];
}
-(无效)视图卸载{
[自设置分段控制:无];
[控制释放];
分段控制=零;
[超级视频下载];
}
@结束

我看到的唯一问题是,您使用的视图控制器中有一个表视图,但您没有在
视图中为其设置委托,也没有为视图控制器实现委托

@interface MessageViewController () <UITableViewDelegate, UITableViewDataSource>

@end
还要确保在IB中正确连接了所有内容,包括分段控件和表视图


编辑:我根据您尝试执行的操作进行了自己的测试,

设置委托和数据源方法,并使用断点检查数据。你的代码是对的

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

我不知道为什么它最终不起作用,我所做的只是删除了三个类,并用上面的代码重做了所有的事情,一些东西一定是在途中被破坏了,但它现在起作用了,我是一个快乐的露营者。也不需要委托的东西,因为这些都是在nib中完成的,所以我的原始代码运行良好。

分段器中设置断点。您看到执行该方法的程序了吗?如果是这样,您是否看到self.segmentControl.selectedSegmentIndex的不同值?当它们都在做相同的事情时,为什么需要在
分段器中切换?此外,您还可以输入一些
NSLog
s来查看程序正在执行的操作。我猜,插座设置为条形按钮项,而不是其内部的分段控件。在文档导航器中检查它实际连接到的对象。我在分段器中放入日志,它们都会弹出,这样工作正常。我真的不需要开关,我想这是多余的,所以我删除了它。然后,我在-(UITableViewCell*)tableView:(UITableView*)或tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath中放入一个日志{但是,当按下分段控制时,从未触发。我检查了插座,它被设置为分段控制,而不是条形项目。谢谢你的回答。我更新了上面的代码,但它仍然不工作。好的,另一个建议,这通常是好的做法。不要直接调用IVAR(tableView.method)但是,请使用您定义的self.tableView.method访问器方法。还要扩展UITableViewDataSource并对其进行设置。好的,我做了自己的测试并使其正常工作。为分段控件设置iAction时,请确保它位于值更改事件上。我在nib中通过将表视图连接到文件所有者来完成此操作,但我刚刚添加了
tableview.delegate=self;
 tableView.datasource=self;