use*_*173 1 objective-c uitableview ios
我正在尝试将重新排序的单元格索引转换为数组.
在 viewDidLoad
cell.showsReorderControl = YES;
arrayTag = [NSMutableArray array];
Run Code Online (Sandbox Code Playgroud)
//在创建单元格时将indexPath.row保存为cell.tag.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.....
cell.tag = indexPath.row;
NSString *strCellTag = [NSString stringWithFormat:@"%d",cell.tag];
if(![arrayTag containsObject:strCellTag])
{
[arrayTag addObject:strCellTag];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
//我不确定这是不是正确的方法,但下面我试图将更改保存到数组中.
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
[arrayTag replaceObjectAtIndex:sourceIndexPath.row withObject:[NSString stringWithFormat:@"%i",proposedDestinationIndexPath.row]];
[arrayTag replaceObjectAtIndex:proposedDestinationIndexPath.row withObject:[NSString stringWithFormat:@"%i",sourceIndexPath.row]];
return proposedDestinationIndexPath;
}
Run Code Online (Sandbox Code Playgroud)
验证我NSLog:
- (IBAction)doneButton:(id)sender
{
NSLog (@"Number of Objects in Array %i", arrayTag.count);
for (NSString *obj in arrayTag){
NSLog(@"From ArrayTag obj: %@", obj);
}
}
Run Code Online (Sandbox Code Playgroud)
NSLog 没有移动单元格的结果如预期.
2013-07-10 17:50:47.291 MyApp[1634:c07] Number of Objects in Array 7
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 0
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 1
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 2
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 3
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 4
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 6
Run Code Online (Sandbox Code Playgroud)
NSLog移动/洗牌后细胞似乎是错误的.我没有得到独特的价值观.
2013-07-10 17:51:55.329 MyApp[1634:c07] Number of Objects in Array 7
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 4
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 6
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 6
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 4
Run Code Online (Sandbox Code Playgroud)
问:为什么重新排序后我没有获得新的独特价值?如何获取保存在数组中的新单元格顺序?
在以下期间,您不应该修改tableView背后的数据:
-tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
Run Code Online (Sandbox Code Playgroud)
当用户将单元格悬停在其他单元格上方时,会调用此方法,因此,在拖动单元格时会导致多次调用此函数.
这里的关键是,该方法是其中的一部分UITableViewDelegate,它并不意味着修改数据,而是改变tableView的外观,而它就是在单元格悬停时执行"滑动"动画.返回的值确定应将单元格设置为动画的位置.
你应该做的是,当用户提交它们时执行更改,当你接到调用UITableViewDataSource协议方法时会发生这种情况:
-tableView:moveRowAtIndexPath:toIndexPath:
Run Code Online (Sandbox Code Playgroud)
这是当用户释放单元格并且它就位时,每次拖放就会发生一次.
作为一般的经验法则,UITableViewDelegate对于节目而言,无论是什么,以及UITableViewDataSource真实数据的用途是什么.
交换或交换是最简单的情况([ A,B,C,D ] => [ D,B,C,A ]),并且有一个非常方便的方法:
[arrayTag exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
Run Code Online (Sandbox Code Playgroud)
迅速
let f = fromIndexPath.row, t = toIndexPath.row
(array[f], array[t]) = (array[t], array[f])
Run Code Online (Sandbox Code Playgroud)
插入有点棘手和冗长([ A,B,C,D ] => [B,C,D,A ]).重要的是进行更改,使它们不会相互影响.
id obj = [arrayTag objectAtIndex:fromIndexPath.row];
[arrayTag removeObjectAtIndex:fromIndexPath.row];
[arrayTag insertObject:obj atIndex:toIndexPath.row];
Run Code Online (Sandbox Code Playgroud)
迅速
let obj = array.removeAtIndex(fromIndexPath.row)
array.insert(newElement: obj, atIndex: toIndexPath.row)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2297 次 |
| 最近记录: |