【问题标题】:iOS reload UITableView using UISegmentedControliOS 使用 UISegmentedControl 重新加载 UITableView
【发布时间】:2012-08-11 05:18:47
【问题描述】:

您好,我正在尝试根据两个不同的数组重新加载我的表格视图。应该加载哪个数组由导航栏中的段控件确定。目前它只会加载第一个数组,按下段控件时不会发生任何事情。以下是我的代码,非常感谢任何关于为什么这不起作用的帮助。我还检查了我的 IBAction 分段器是否连接在笔尖中。

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 的不同值?
  • segmenter 都在做同样的事情,为什么还需要开关?你也可以输入一些NSLogs 来看看程序在做什么。
  • 这是我的猜测 - 出口设置为栏按钮项目,而不是位于其中的分段控件。在文档导航器中检查它实际连接到的对象。
  • 我在分段器中输入了日志,它们都弹出了,所以工作正常我真的不需要开关,我想这真的是多余的,所以我删除了它。然后我输入了一个日志 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 但是当按下段控件时永远不会触发
  • 我检查了插座,它设置为段控件而不是栏项

标签: ios uitableview uisegmentedcontrol


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我在 nib 中通过将表格视图连接到文件所有者来做到这一点,但是我刚刚也将它添加到了我的 viewdidload 但仍然没有工作
    • if(segmentControl.selectedSegmentIndex == 0) { cell.textLabel.text = [inbox objectatIndex:IndexPath.row];试试看……
    【解决方案3】:

    我看到的唯一问题是您正在使用一个带有表格视图的视图控制器,但您没有在 viewDidLoad 中为它设置委托,并且您没有实现委托你的视图控制器。

    @interface MessageViewController () <UITableViewDelegate, UITableViewDataSource>
    
    @end
    

    viewDidLoad:

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

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

    编辑:我根据您的尝试做了自己的测试,Here's my own implementation file

    【讨论】:

    • 感谢您的回答我更新了上面的代码,但它仍然无法正常工作
    • 好的,另一个建议,这通常是好的做法。不要直接调用您的 ivars (tableView.method),而是使用您定义的 self.tableView.method 的访问器方法。同时扩展 UITableViewDataSource 并设置它。
    • 好的,我做了自己的测试并让它工作。当您为分段控件设置 IBAction 时,请确保它位于 Value Changed 事件中。
    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    相关资源
    最近更新 更多