【问题标题】:Populating a UITableView within a UIViewController and changing data source with UISegmentedControl在 UIViewController 中填充 UITableView 并使用 UISegmentedControl 更改数据源
【发布时间】:2015-08-19 00:12:43
【问题描述】:

我正在尝试填充一个UITableView,我将其放入UIViewController 中,位于UISegmentedControl 下方,如图所示:

按下任何段后,我希望UITableView 重新填充一组不同的数据。这是我的相关代码:

@interface OTValuesViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;

@end

@implementation OTValuesViewController


- (IBAction)segmentValueChanged:(id)sender {
    UISegmentedControl *segment = self.segmentControl;
    UINavigationItem *navItem = self.navigationItem;
    UIBarButtonItem *addButton = navItem.rightBarButtonItem;
    switch (segment.selectedSegmentIndex) {
        case 0:
            addButton.action = @selector(addNewVision:);
            [self.tableView reloadData];
            break;
        case 1:
            addButton.action = @selector(addNewPurpose:);
            [self.tableView reloadData];
            break;
        case 2:
            addButton.action = @selector(addNewPlan:);
            [self.tableView reloadData];
            break;
        default:
            [self.tableView reloadData];
            break;

    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UISegmentedControl *segment = self.segmentControl;
    UITableViewCell *cell = [tableView     dequeueReusableCellWithIdentifier:@"OTValuesTableViewCell" forIndexPath:indexPath];

    NSArray *visions = [[OTVisionStore sharedStore] allVisions];
    NSArray *purposes = [[OTPurposeStore sharedStore] allPurposes];
    NSArray *plans = [[OTPlanStore sharedStore]allPlans];

    OTVision *vision = [visions objectAtIndex:indexPath.row];
    OTVision *purpose = [purposes objectAtIndex:indexPath.row];
    OTVision *plan = [plans objectAtIndex:indexPath.row];

    NSString *textLabel = [vision description];

    if (segment.selectedSegmentIndex==0) {
        textLabel = [vision description];
        cell.textLabel.text = textLabel;
    }
    else if (segment.selectedSegmentIndex==1) {
        textLabel = [purpose description];
        cell.textLabel.text = textLabel;
    }
    else{
        textLabel = [plan description];
        cell.textLabel.text = textLabel;
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    UISegmentedControl *segment = self.segmentControl;
    if(segment.selectedSegmentIndex==0) {
        return [[[OTVisionStore sharedStore]allVisions]count];
    }
    else if (segment.selectedSegmentIndex==1) {
        return [[[OTPurposeStore sharedStore]allPurposes]count];
    }
    else{
        return [[[OTPlanStore sharedStore]allPlans]count];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    self.title=@"Mission Statement";

    UINavigationItem *navItem = self.navigationItem;

    UIBarButtonItem *rbbi = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                     target:self
                                                                     action:@selector(addNewVision:)];
    navItem.rightBarButtonItem = rbbi;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

我的问题是:当我构建和运行项目时,一切都很好——单元格填充在我的默认选项卡(视觉)中。当我单击 UISegmentedControl 按钮时,我收到错误消息:

由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]”

如果我删除:

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

从我的viewDidLoad 来看,不会发生此错误(尽管 UITableView 显然没有被填充。)这使我相信该错误是由于我根据 UISegmentedController's 填充单元格而产生的选定的段,但是,我不知道具体是什么原因造成的。

任何帮助将不胜感激!

【问题讨论】:

    标签: ios objective-c uitableview uisegmentedcontrol


    【解决方案1】:

    问题出在您的cellForRowAtIndexPath 方法中。您尝试对所有数组使用当前索引路径,而不仅仅是适用的数组。试试这样的:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UISegmentedControl *segment = self.segmentControl;
        UITableViewCell *cell = [tableView     dequeueReusableCellWithIdentifier:@"OTValuesTableViewCell" forIndexPath:indexPath];
    
        if (segment.selectedSegmentIndex==0) {
            NSArray *visions = [[OTVisionStore sharedStore] allVisions];
            OTVision *vision = [visions objectAtIndex:indexPath.row];
            NSString *textLabel = [vision description];
            cell.textLabel.text = textLabel;
        }
        else if (segment.selectedSegmentIndex==1) {
            NSArray *purposes = [[OTPurposeStore sharedStore] allPurposes];
            OTVision *purpose = [purposes objectAtIndex:indexPath.row];
            NSString *textLabel = [purpose description];
            cell.textLabel.text = textLabel;
        }
        else{
            NSArray *plans = [[OTPlanStore sharedStore]allPlans];
            OTVision *plan = [plans objectAtIndex:indexPath.row];
            NSString *textLabel = [plan description];
            cell.textLabel.text = textLabel;
        }
    
        return cell;
    }
    

    【讨论】:

    • 这完美解决了我的问题!非常感谢——我真的为这个而摸不着头脑。
    猜你喜欢
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多