データ駆動型の TableView をカスタマイズする
-
20-09-2019 - |
質問
1 つのセクションに XML データが入力されたグループ化されたテーブルビューがあります。私がやりたいのは、データドリブンセクションの前に別のセクションを作成し、それにアクションを適用することです。
例:
ユーザーには「現在地を使用」というボタンが表示され (手動で作成されたセクション)、その下にはユーザーが代わりに選択できる国のリスト (データ主導型セクション) が表示されます。
設定メニューをガイドとして使用してください。セクション内に 1 行のオプションがいくつかあるため、ボタンのように見えます。
これで意味が分からない場合は、より適切に説明するよう努めます。
したがって、これらの明らかなコード行が 2 行あります...十分に単純です
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [countrysData count];
}
私が欲しいのは、 numberOfSectionsInTableView
2 を返し、最初の「セクション」に「クリックして現在地を使用する」と表示すると、地図が表示され、2 番目のセクションには現在作業している国のリストが表示されます。
解決
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0){ return 1; }else{ return [countrysData count]; } }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
あなたはあなたのdidSelectRowAtIndexPathに何をすべきかを選択する必要があります:indexPath.sectionによる方法。ああ、あなたはあなたのcellForRowAtIndexPathでindexPath.sectionを確認してください:メソッドを。
他のヒント
新しいセクションとその行を適切に考慮するために、UITableViewDataSource プロトコルと UITableViewDelegate プロトコルのすべての実装を更新するだけです。
たとえば、更新方法は次のとおりです TableView のセクション数:, テーブルビュー:セクションの行数:, 、 そして tableView:cellForRowAtIndexPath:, 、しかし、少なくとも更新する必要があります tableView:didSelectRowAtIndexPath: 同じように:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// calculate the number of sections of non-data (might just be 1)
// calculate the number of sections for the data (you were already doing this, might just be 1)
// return the sum
return 1 + 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rowCount = 0;
switch (section) {
case 0:
// non-data section has 1 row/cell
rowCount = 1;
break;
case 1:
// data section uses an array
rowCount = [dataArray count];
break;
}
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *nonDataCellID = @"NonDataCell";
static NSString *dataCellID = @"DataCell";
UITableViewCell *cell;
int section = [indexPath indexAtPosition:0];
int row = [indexPath indexAtPosition:1];
switch (section) {
case 0:
// or you can just use standard cells here
cell = [tableView dequeueReusableCellWithIdentifier:nonDataCellID];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"NonDataCell" owner:self options:NULL];
cell = nonDataCell; // nonDataCell is an IBOutlet to this custom cell
}
// configure non-data cell here (use tags)
UILabel *someLabel = (UILabel*)[cell viewWithTag:1];
someLabel.text = @"Non-data cell";
break;
case 1:
// or you can just use standard cells here
cell = [tableView dequeueReusableCellWithIdentifier:dataCellID];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"dataCell" owner:self options:NULL];
cell = dataCell; // dataCell is an IBOutlet to this custom cell
}
// configure data call here (using "row")
UILabel *someDataLabel = (UILabel*)[cell viewWithTag:1];
someDataLabel.text = [[dataArray objectAtIndex:row] valueForKey:@"name"];
break;
}
return cell;
}
私はあなたがUITableViewDataSourceのメソッド「numberOfSectionsInTableView」のリターンを変えることができますかなり確信しています。ユーザーが追加のセクションの選択を選択すると、ちょうどこの方法は、あなたがしたいテーブルの数を返すようにフラグを設定します。その後、テーブルのリロードを強制して、新しいセクションが表示されるはずです。