我希望能够将一个单元格拖到另一个单元格上,当我释放它时,它会有效地“合并”单元格(显示不同的单元格)。我不确定UITableView
是最佳控件还是UICollectionView
。有没有人对如何进行有任何想法?
答案 0 :(得分:4)
可能是我没有得到正确但是因为我得到了,你想通过拖动来合并单元格,所以为你实现一些代码。看看这个。
如果你想要这样的东西......
使用这样的编辑代码:---
ViewDidLoad初始化数组并将表设置为编辑模式:
fruitsArray=[[NSMutableArray alloc] initWithObjects:@"Apple",@"Banana",@"Mango",@"Guava",@"PineApple",@"Watermelon",@"Grapes",@"GroundNut",@"Muskmelon",@"Orange",@"Cherry",nil];
[tblView setEditing:YES];
tableView的数据源设置为: - >
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if (sourceIndexPath.row!=destinationIndexPath.row) {
NSString *sourceString=[fruitsArray objectAtIndex:sourceIndexPath.row];
NSString *destinationString=[fruitsArray objectAtIndex:destinationIndexPath.row];
destinationString=[destinationString stringByAppendingFormat:@",%@",sourceString];
[fruitsArray replaceObjectAtIndex:destinationIndexPath.row withObject:destinationString];
[fruitsArray removeObjectAtIndex:sourceIndexPath.row];
[tblView reloadData];
}
}
试试这个sampleCode
答案 1 :(得分:2)
以下是我用伪代码开发它的解决方案。
UILongPressGestureRecognizer
添加到UITableView UIGestureRecognizerStateBegan
上,使用[UITableView indexPathForRowAtPoint]
以确定“长按”的单元格。您将此单元格转换为悬停 UITableViewCell
。UIGraphicsBeginImageContextWithOptions
呈现实际UITableViewCell内容的图像。创建UIImageView
并为其添加一些自定义镶边(阴影等)UIGestureRecognizerStateChanged
上,计算UITableViewCell
中悬停 UITableView
的位置,并为固定单元设置动画。 (您还需要切换先前突出显示的固定单元格的动画。)UITableView
全部调用。还有一些需要注意的事项,例如当您遇到边界条件时滚动,将悬停单元格放在原始位置等等,但这是它的基本要点。希望这会有所帮助。
答案 2 :(得分:1)
我使用长按手势。这是示例代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
longPress.delegate = self;
[cell addGestureRecognizer:longPress];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
[gestureRecognizer addTarget:self action:@selector(moveRight:)];
}
return YES;
if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
[gestureRecognizer addTarget:self action:@selector(tapAction:)];
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)moveRight:(UILongPressGestureRecognizer *)sender {
/*if (sender.view.tag==4)
{
[super setEditing:YES animated:YES];
[self.Table4 setEditing:YES animated:YES];
}
else
{
*/
UIView *view = sender.view;
int t = sender.view.tag;
CGPoint point = [sender locationInView:view.superview];
NSLog(@"its added %f", point.x);
NSLog(@"its added %f", point.y);
// view.frame = CGRectMake(sender.view.frame.origin.x, sender.view.frame.origin.y+200,200, 30);
CGPoint center = view.center;
center.x = point.x ;
center.y = point.y ;
view.center = center;
[self.view addSubview:view];
// int x = Table4.frame.origin.x;
// int y = Table4.frame.origin.y;
if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint center = view.center;
center.x += point.x - _priorPoint.x;
center.y += point.y - _priorPoint.y;
NSLog(@"its Center %f", center.x);
if (center.x > Table4.frame.origin.x &&
center.x < Table4.frame.origin.x +
Table4.frame.size.width &&
center.y > Table4.frame.origin.y &&
center.y < Table4.frame.origin.y +
Table4.frame.size.height
)
{
int count = [self.rowdata count];
if (t>=100 && t<200)
{
t=t-100;
[self.rowdata insertObject:[listData objectAtIndex:t] atIndex:count];
}
else if (t>=200 && t<300)
{
t=t-200;
[self.rowdata insertObject:[listData2 objectAtIndex:t] atIndex:count];
}
else
{
t=t-300;
[self.rowdata insertObject:[listData3 objectAtIndex:t] atIndex:count];
}
//[self.rowdata addObject:[listData objectAtIndex:t]];
//[sender release];
[Table4 reloadData];
[Table1 reloadData];
[Table2 reloadData];
[Table3 reloadData];
}
view.center = center;
//}
_priorPoint = point;
}
}
我认为这对你有帮助。从中获取您想要的代码。