ios - 想要使用相同的 TableView 一次从两个数组填充数据

标签 ios objective-c

我是 iOS 应用程序开发的新手,所以需要一点支持!! 我在 objectiveC 中使用了一个 TableView 来制作一个下拉列表以显示已成功制作的国家/地区名称,但现在我希望将同一个 tableView 用于不同的州列表,因为我有两个NSMutable 数组一个用于“countryList”,另一个用于“stateList” 我可以使用相同的 tableView 但显示 stateList 数据吗?

NSMutableArray *countryArray;
NSMutableArray *stateArray;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return countryArray.count ;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   return 25;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;

    static NSString *idenfier = @"countryList";
    cell = [tableView dequeueReusableCellWithIdentifier:idenfier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [countryArray objectAtIndex:indexPath.row];

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.registerCountry.text = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
    // self.countryView.hidden = YES;

    self.countryView.frame = CGRectMake(self.countryView.frame.origin.x, self.countryView.frame.origin.y, self.countryView.frame.size.width, 0);

    if([_registerCountry.text isEqual:@""]){
        [_dropDownState setEnabled:NO];
    }
    else{
        [_dropDownState setEnabled:YES];

    }

}

- (IBAction)dropDownCountry:(id)sender {
    self.countryView.hidden = NO;

    if (self.countryView.frame.size.height != 0) {
        [UIView animateWithDuration:0.3 animations:^{
            self.countryView.frame = CGRectMake(self.countryView.frame.origin.x, self.countryView.frame.origin.y, self.countryView.frame.size.width, 0);
        } completion:^(BOOL finished) {
            // self.dropDownImageView.highlighted = NO;
        }];
    }
    else {
        [UIView animateWithDuration:0.3 animations:^{
            self.countryView.frame = CGRectMake(10, 404, 302, 100);
        } completion:^(BOOL finished) {
            // self.dropDownImageView.highlighted = YES;
        }];
    }

}

- (IBAction)dropDownState:(id)sender {
}

最佳答案

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
     return stateList.count
     //OR
     return countryList.count
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Load cell with the array[indexPath.row] you want
}

关于ios - 想要使用相同的 TableView 一次从两个数组填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269342/

相关文章:

objective-c - 弹出式按钮单元使用 Cocoa Binding 返回 bool 值

ios - ViewController 偶尔会错过响应链?

ios - Swift:如何优化我编写的用于在裁剪后调整图像大小的代码?

ios - 将它们存储在 UserDefault 中时如何处理模型迁移?

objective-c - iOS 4.3 的 NSDate 完整日期

ios - 在“init”中初始化的对象无法在“viewDidLoad”中访问

objective-c - 为什么不调用dealloc?

objective-c - UIScrollView 仅在底部禁用垂直弹跳

ios - NSMutableArray ReplaceObjectAtIndex 崩溃

objective-c - 如何从 UISwitch 中删除目标,以避免因快速点击开关而产生的问题?