當 TableView 每行顯示四張照片時,所需行數等於照片總數除以四並向上取整。Objective-C 的整數除法會直接捨去小數,因此可用 (count + 3) / 4 來實現無條件進位。
static const int kImagesPerRow = 4;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (arryData.count + kImagesPerRow - 1) / kImagesPerRow;
}
這個公式可適用於任何每行固定項目數的佈局計算。