Tweetbot が次のことをどのように行うのか興味があります。
私のアプリでも同じものを作成したいと思います。行をクリックすると追加の UIToolBar がポップアップし、他の行を押すとアニメーションでこのビューが閉じられます。
ロジックは単純だと思います。押されたときに UITableViewCell にサブビューを追加し、残りのコンテンツを上にシフトするだけですが、他の行を押したときに実際にそれをどのように閉じるのでしょうか。
ベストアンサー1
これを行う最善の方法は、タップされたセルの下にダミー セルを追加することです。
まず、どのセルがタップされたかを追跡し、それに応じて行動する必要があります。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//if user tapped the same row twice let's start getting rid of the control cell
if([indexPath isEqual:self.tappedIndexPath]){
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
//update the indexpath if needed... I explain this below
indexPath = [self modelIndexPathforIndexPath:indexPath];
//pointer to delete the control cell
NSIndexPath *indexPathToDelete = self.controlRowIndexPath;
//if in fact I tapped the same row twice lets clear our tapping trackers
if([indexPath isEqual:self.tappedIndexPath]){
self.tappedIndexPath = nil;
self.controlRowIndexPath = nil;
}
//otherwise let's update them appropriately
else{
self.tappedIndexPath = indexPath; //the row the user just tapped.
//Now I set the location of where I need to add the dummy cell
self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
}
//all logic is done, lets start updating the table
[tableView beginUpdates];
//lets delete the control cell, either the user tapped the same row twice or tapped another row
if(indexPathToDelete){
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete]
withRowAnimation:UITableViewRowAnimationNone];
}
//lets add the new control cell in the right place
if(self.controlRowIndexPath){
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
//and we are done...
[tableView endUpdates];
}
ダミー セルが存在する場合は、必ず正しいカウントを送信する必要があります。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.controlRowIndexPath){
return modelArray.count + 1;
}
return self.modelArray.count;
}
また、ControlCell の適切な高さを返します。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath isEqual:self.controlRowIndexPath]){
return 45; //height for control cell
}
return 70; //height for every other cell
}
最後に、コントロール セルはダミーであることを忘れないでください。モデルの一部ではないため、その点を考慮する必要があります。ユーザーが最後にタップした行の上にある行をタップした場合は問題ありませんが、新しくタップした行がそのコントロール セルの下にある場合は、モデル内の正しい行にアクセスしていることを確認する必要があります。つまり、ビューの中央にある偽のセルを考慮する必要があります。
- (NSIndexPath *)modelIndexPathforIndexPath:(NSIndexPath *)indexPath
{
int whereIsTheControlRow = self.controlRowIndexPath.row;
if(self.controlRowIndexPath != nil && indexPath.row > whereIsTheControlRow)
return [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0];
return indexPath;
}
これが役に立つことを願っています。