cell
にボタンが付いているのは 1 つだけtableview
です。cell
高さは
tableView:tableView:heightForRowAtIndexPath:
セルの高さは 40 です。
ボタンをクリックしたときに、セルの高さを 80 に変更するにはどうすればよいですか? 助けてくれてありがとう。
プロパティを定義すると、次のようになり@synthesize
ます。
@property (retain, nonatomic) NSIndexPath* selectedRowIndex;
オーバーライドdidSelectRowAtIndexPath
とheightForRowAtIndexPath
メソッド:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {
//[_yourTableView deselectRowAtIndexPath:indexPath animated:YES]; If you only want the cell to get bigger
selectedRowIndex = nil;
}
else { self.selectedRowIndex = [indexPath retain]; }
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == _yourTableView) {
if(selectedRowIndex && indexPath.row == selectedRowIndex.row){
return 80;
}
// Cell isn't selected so return single height
return 40;
}
else return 0;
}
beginUpdates および endUpdates 行のため、これはセルの高さをアニメーション化する必要があります。
UITableView デリゲートのドキュメントは次のとおりです。デリゲートを設定し、関数を (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath に設定する必要があります。戻り値は CGFloat であることを忘れないでください。したがって、80 ではなく 80.0 を返します。
ボタンが押されたら、それが押されたことを示す値に変数を設定してから、tableViewでreloadDataを呼び出す必要があります。これにより、tableView:heightForRowAtIndexPathが再度呼び出されます。
tableView:heightForRowAtIndexPathで、ボタンが押されたかどうかを示す変数を確認し、ボタンが押された場合は80を返します。