利用从 this answer 填充 UITableView 的方法,并通过以下方式将表设置为编辑模式:
tableView.editing = true;
并添加这些方法:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
}
我发现用户可能会不小心(或有意)双击排序句柄并插入一个空行。这也将终止 TableView 的滚动。这是一张图片:
在“日志”和“设置”之间有一个空行。我已经通过 the answers found here 遍历了 TableView 中的所有行, 并且空行被跳过,就好像它们不存在一样。
每次移动一行时,我都能够通过 [tableView reload] 删除空行,不幸的是,这会导致一种不和谐的快照。但是,我无法重新启用滚动。我试图避免必须让用户关闭并重新显示 View 。
有人遇到过这个问题并找到了合适的解决方法吗?
编辑:
行删除无关紧要 - 这是移动行的结果
这是 ios 8/7 上的 Xcode 6.1 - 也看到了这个 Xcode 5.x -> ios 7/8。 我们的目标是 ipad,所以不确定这是否是 iphone 上的问题
此外,更好地描述如何重现:
- 双击单元格并在第二次点击时按住一秒钟
- 向上或向下拖动单元格
Posted on Apple forums并向 Apple 提交了错误。这是我提交给他们的示例项目(我能够在其中重现上述问题):
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableData = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five",
@"One",@"Two",@"Three",@"Four",@"Five", nil];
self.tableView.editing = true;
[self.tableView setTableFooterView:[UIView new]];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.tableView.rowHeight = 20.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ScrollingDisplayCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( cell == nil )
{
cell = [[[UITableViewCellNoPadding alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; // MEMCHK: Autorelease so no ownership implications
cell.textLabel.font = [UIFont fontWithName:@"Courier New" size:17.0];
cell.textLabel.frame = cell.contentView.bounds;
}
// Configure the cell...
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = self.tableData[sourceIndexPath.row];
[self.tableData removeObjectAtIndex:sourceIndexPath.row];
[self.tableData insertObject:stringToMove atIndex:destinationIndexPath.row];
}
这是 UITableViewCellNoPadding : UITableViewCell:
// https://stackoverflow.com/questions/3467288/center-align-text-in-uitableviewcell-problem
-(void)layoutSubviews
{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height);
}
注意:在这里重现比在我自己的应用中更难
最佳答案
由于您指定用户不应删除行,因此我添加了 editingStyleForRowAtIndexPath
以始终返回 UITableViewCellEditingStyleNone
。如果没有此实现,则默认返回 UITableViewCellEditingStyleDelete
。
通过下面发布的实现,一切似乎都运行良好,并且我已检查所有编辑是否已成功提交到表模型。
希望对您有所帮助!
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableModel = [NSMutableArray new];
for (int i = 0; i < 30; i++) {
[self addTableObject];
}
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.editing = true;
}
- (void) addTableObject
{
NSString *rowValue = [NSString stringWithFormat:@"%i", _tableModel.count+1];
[_tableModel addObject:rowValue];
}
#pragma mark - Table view delegate methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_tableModel objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _tableModel.count;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//Make change in data model
id selectedValue = [_tableModel objectAtIndex:sourceIndexPath.row];
[_tableModel removeObjectAtIndex:sourceIndexPath.row];
[_tableModel insertObject:selectedValue atIndex:destinationIndexPath.row];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
关于ios - UITableView 正在添加空行并禁用滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26896957/