これは、行数を動的に返す方法が必要だからです。
たとえば、3 つの配列を作成します。それぞれに 3 つの値があります (これらはNSArray
変数です):
ファイル.h
内:
NSArray *firstArray;
NSArray *secondArray;
NSArray *thirdArray;
ファイルでは.m
、viewDidLoad または init または同様のもの:
firstArray = [NSArray arrayWithObjects:@"Cat", @"Mouse", @"Dog", nil];
secondArray = [NSArray arrayWithObjects:@"Plane", @"Car", @"Truck", nil];
thirdArray = [NSArray arrayWithObjects:@"Bread", @"Peanuts", @"Ham", nil];
テーブルの行数を返すとき、私は持っています:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array.count;
if (section == 0) {
return firstArray.count;
} else if (section == 1) {
return secondArray.count;
} else {
return thirdArray.count;
}
}
次に、でcellForRow
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if (indexPath.section == 0) {
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
return cell;
}
次に@"Dog"
、テーブルをスワイプするか、他の方法で削除します。次に、テーブルをリロードすると、配列カウントが 2 になるため、テーブルは 2 行のみを表示する必要があることを「認識」します。基本的に、データ ソースも更新する必要があります。他のセクションにも適用されます。配列から要素を削除するため、行数も更新されます。