【问题标题】:Want to populate data from two array one at a time using same TableView想要使用相同的 TableView 一次从两个数组中填充数据
【发布时间】:2016-09-02 18:12:47
【问题描述】:

我是 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 {
}

【问题讨论】:

  • 请在此处复制粘贴您的代码以提供更多详细信息
  • 绝对有可能。发布一些代码作为亚历山大笔记,或发布您的数据源。
  • 你想如何显示国家和州列表?意味着它会改变哪个动作?你能提供那个图片..

标签: ios objective-c


【解决方案1】:
@interface RootViewController : UIViewController {
UIButton *btnCountry;
UIButton *btnState;
NSMutableArray *tempArray;
NSMutableArray *countryArray;
NSMutableArray *stateArray;
IBOutlet UITableView *tempTable;
}

@property (nonatomic,retain) UIButton *btnCountry;
@property (nonatomic,retain) UIButton *btnState;
@property (nonatomic,retain) NSMutableArray *countryArray;
@property (nonatomic,retain) NSMutableArray *stateArray;
@property (nonatomic,retain) NSMutableArray *tempArray;
@property (nonatomic,retain) UITableView *tempTable;

- (IBAction) showState:(id)sender;
- (IBAction) showCountry:(id)sender;

这里是 .m 代码

@implementation RootViewController

@synthesize btnState,btnCountry, stateArray,countryArray,tempArray;
@synthesize tempTable;

- (void)viewDidLoad {

[super viewDidLoad];

tempTable.hidden = YES;

countryArray = [[NSMutableArray alloc]initWithObjects:@"India",@"Pakistan",@"USA",nil];
stateArray = [[NSMutableArray alloc]initWithObjects:@"Gujarat",@"Maharashtra", @"Karnataka",nil];
tempArray = [[NSMutableArray alloc]init];

}

- (IBAction) showCountry:(id)sender
{
btnCountry = (UIButton *)sender;
tempArray = countryArray;
[tempTable reloadData];
if([btnCountry isSelected])
{
    tempTable.hidden = YES;
    btnCountry.selected = NO;
}
else
{
    tempTable.hidden = NO;
    btnCountry.selected = YES;
}
}


- (IBAction) showState:(id)sender
{
btnState = (UIButton *)sender;
tempArray = stateArray;
[tempTable reloadData];

if([btnState isSelected])
{
    tempTable.hidden = YES;
    btnState.selected = NO;
}
else
{
    tempTable.hidden = NO;
    btnState.selected = YES;
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      {

return [tempArray count];
 }


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];

return cell;
}


@end

【讨论】:

【解决方案2】:
- (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
}

【讨论】:

    【解决方案3】:

    是的,可以只输入 isCountry BOOL 并签入表格视图委托和数据源。当您想更改 tableView 值时,只需更改 isCountryreloadtableView 的值。

    【讨论】:

      【解决方案4】:

      是的,只需使用一个BOOL 变量来区分countryListstateList 下拉列表。

      BOOL isCountyDropDownClick;

      当任何一个选项点击时,相应地设置BOOLReload TableView

      喜欢,如果点击 countryList,则将BOOL 设置为YESReload table,否则将BOOL 设置为NO

      numberOfRowsInSectioncellForRowAtIndexPath根据BOOL值使用数据源数组。

      例如,

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
      
          if (isCountyDropDownClick)
          {
      
             return  CountryListArray.count;
      
          }
          else{
      
             return  StateListArray.count;
      
          }
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          if (isCountyDropDownClick)
          {
      
             //Use CountryListArray
      
          }
          else{
      
            //Use StateListArray
      
         }
      
         .....
      }
      

      这可能会给出一些想法并根据您的要求进行修改。

      【讨论】:

        【解决方案5】:

        您可以使用 UITableView 部分,这样您的国家/地区列表可以是您的部分,而您的州可以是您的行,当部分(国家)列表将关闭时,该部分中的行可能为 0,当您点击并打开您时可以填充实际的行列表。

        @interface States : NSObject
        
        @property(nonatomic, strong) NSString *name;
        
        @end
        
        @interface Countries : NSObject
        
        @property(nonatomic, strong) NSMutableArray<States *> *states;
        
        @end
        
        
        @interface ViewController () <UITableViewDataSource>
        
        @property(nonatomic,strong) NSMutableArray<Countries *> *countries;
        
        @end
        
        @implementation ViewController
        
        
        -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
            return self.countries.count;
        }
        
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
            return [self.countries objectAtIndex:section].states.count;
        }
        
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        // return your configured cell here
        }
        
        
        @end
        

        在上面的代码中,我创建了两个类,一个用于国家,另一个用于州,以正确映射表格希望这会有所帮助。

        【讨论】:

          【解决方案6】:

          在您的 TableViewController 中定义枚举

          typedef NS_ENUM(NSInteger, ListViewType)
          {
              CountryList,
              StateList
          };
          
          @interface TableViewController : UITableViewController
          
          @property (nonatomic, weak) ListViewType listViewType;
          
          @end
          
          
          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
          
              // Configure the cell...
              switch (self.listViewType)
              {
                  case CountryList:
                      //Load countryList array
                      break;
          
                  case StateList:
                      //load stateList array
                      break;
          
                  default:
                      break;
              }
              return cell;
          }
          

          当前视图控制器设置要加载的列表类型

              TableViewController * tableViewController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
              tableViewController.listViewType = CountryList; //Set depends on the need (CountryList/ StateList)
              tableViewController.modalPresentationStyle = UIModalPresentationFormSheet;
              tableViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
              [self presentViewController:tableViewController animated:YES completion:nil];
          

          【讨论】:

            【解决方案7】:

            您可以使用标签栏作为您的解决方案,

            更改选项卡并在同一张表上显示内容。

            对于那次访问,

            Tab bar display data in same table view

            希望这会对你有所帮助。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-06-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多