連絡先アプリに似たアプリを開発しています。アイテムのリストがあり、各アイテムには詳細ビューがあります。詳細ビューはテーブル ビューで、名前、場所、値などの値が表示されます。ただし、場所に値がない場合は、詳細ビューに空白のセルがないようにセクションを表示したくありません。 . 私は6つのセクションを持っています。値が割り当てられておらず、セクション 5 に 2 つの行がある場合でも、セクション 2 は常に表示されます。他のすべてのセクションには 1 行あります。私は他の質問を見てきました。しかし、私はあまり成功していません。私の現在のコードは次のようになります。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return sectionTitles;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 1) {
if ([[[detailGift givingTo] name] length] == 0) {
return nil;
}
else {
return @"";
}
}
else if (section == 2) {
return @"";
}
else if (section == 3) {
if ([[[detailGift reasonFor] name] length] == 0) {
return nil;
}
else {
return @"";
}
}
else if (section == 4) {
if ([[[detailGift boughtFrom] name] length] == 0) {
return nil;
}
else {
return @"";
}
}
else if (section == 5) {
if ([[formatter stringFromDate:[detailGift dateGiving]] length] == 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] == 0) {
return nil;
}
else {
return @"";
}
}
else {
if ([[detailGift notes] length] == 0) {
return nil;
}
else {
return @"";
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1) {
if ([[[detailGift givingTo] name] length] == 0) {
return 0;
}
else {
return 1;
}
}
else if (section == 2) {
return 1;
}
else if (section == 3) {
if ([[[detailGift reasonFor] name] length] == 0) {
return 0;
}
else {
return 1;
}
}
else if (section == 4) {
if ([[[detailGift boughtFrom] name] length] == 0) {
return 0;
}
else {
return 1;
}
}
else if (section == 5) {
if ([[formatter stringFromDate:[detailGift dateGiving]] length] == 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] == 0) {
return 0;
}
else if ([[formatter stringFromDate:[detailGift dateGiving]] length] != 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] == 0) {
return 1;
}
else if ([[formatter stringFromDate:[detailGift dateGiving]] length] == 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] != 0) {
return 1;
}
else {
// Crashes after returning value of two
return 2;
}
}
else {
if ([[detailGift notes] length] == 0) {
return 0;
}
else {
return 1;
}
}
}
これは私には非常に厄介なようで、次の例外も発生します: Terminating app due to uncaught exception 'NSRangeException', reason: ' * -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
誰かが私にアドバイスをくれたり、私を正しい方向に導いてくれたりしたら、本当に感謝しています! どうもありがとう!スコット