gpt4 book ai didi

iphone - 如何阻止 UITableView moveRowAt IndexPath 在重新排序时留下空白行

转载 作者:行者123 更新时间:2023-12-03 20:11:35 27 4
gpt4 key购买 nike

我遇到一个问题,在重新排序 UITableViewCells 时,tableView 不随单元格滚动。仅出现一个空白行,任何后续滚动都会出现数组越界错误,堆栈跟踪中没有我的任何代码。 Here is a quick video of the problem.

相关代码如下:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath.section == 1;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL ret = indexPath.section == 1 && indexPath.row < self.count;
DebugLog(@"canMoveRowAtIndexPath: %d:%d %@", indexPath.section, indexPath.row, (ret ? @"YES" : @"NO"));
return ret;
}
- (void)delayedUpdateCellBackgroundPositionsForTableView:(UITableView *)tableView {
[self performSelectorOnMainThread:@selector(updateCellBackgroundPositionsForTableView:) withObject:tableView waitUntilDone:NO];
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
if (fromIndexPath.row == toIndexPath.row) return;

DebugLog(@"Moved audio from %d:%d to %d:%d", fromIndexPath.section, fromIndexPath.row, toIndexPath.section, toIndexPath.row);
NSMutableArray *audio = [self.items objectAtIndex:fromIndexPath.section];
[audio exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
[self performSelector:@selector(delayedUpdateCellBackgroundPositionsForTableView:) withObject:tableView afterDelay:kDefaultAnimationDuration/3];
}

这是生成的崩溃堆栈跟踪:

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Application Specific Information:
iPhone Simulator 3.2 (193.3), iPhone OS 3.0 (7A341)
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray removeObjectsInRange:]: index (6) beyond bounds (6)'

Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 CoreFoundation 0x302ac924 ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ + 4
1 libobjc.A.dylib 0x93cb2509 objc_exception_throw + 56
2 CoreFoundation 0x3028e5fb +[NSException raise:format:arguments:] + 155
3 CoreFoundation 0x3028e55a +[NSException raise:format:] + 58
4 Foundation 0x305684e9 _NSArrayRaiseBoundException + 121
5 Foundation 0x30553a6e -[NSCFArray removeObjectsInRange:] + 142
6 UIKit 0x30950105 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow] + 862
7 UIKit 0x30947715 -[UITableView layoutSubviews] + 250
8 QuartzCore 0x0090bd94 -[CALayer layoutSublayers] + 78
9 QuartzCore 0x0090bb55 CALayerLayoutIfNeeded + 229
10 QuartzCore 0x0090b3ae CA::Context::commit_transaction(CA::Transaction*) + 302
11 QuartzCore 0x0090b022 CA::Transaction::commit() + 292
12 QuartzCore 0x009132e0 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 84
13 CoreFoundation 0x30245c32 __CFRunLoopDoObservers + 594
14 CoreFoundation 0x3024503f CFRunLoopRunSpecific + 2575
15 CoreFoundation 0x30244628 CFRunLoopRunInMode + 88
16 GraphicsServices 0x32044c31 GSEventRunModal + 217
17 GraphicsServices 0x32044cf6 GSEventRun + 115
18 UIKit 0x309021ee UIApplicationMain + 1157
19 XXXXXXXX 0x0000278a main + 104 (main.m:12)
20 XXXXXXXX 0x000026f6 start + 54

注意,数组越界长度不是我的元素的长度(我有 9 个),但总是更小的值。

我已经尝试解决这个问题很多 小时 天了,但没有成功……有什么想法吗?


<小时/>更新:根据要求提供更多代码
在我的委托(delegate)中:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
int count = [(UAPlaylistEditDataSource *)self.dataSource count];
if (proposedDestinationIndexPath.section == 0) {
return [NSIndexPath indexPathForRow:0 inSection:sourceIndexPath.section];
}else if (proposedDestinationIndexPath.row >= count) {
return [NSIndexPath indexPathForRow:count-1 inSection:sourceIndexPath.section];
}
return proposedDestinationIndexPath;
}

…就是这样。我正在使用 Three20 框架,到目前为止我还没有遇到任何重新排序问题。问题也不在 updateCellBackgroundPositionsForTableView: 方法中,因为当它被注释掉时它仍然崩溃。

最佳答案

SO...实际上问题出在 Three20 框架中。TTTableView 是 UITableView 的子类,有两个有问题的方法:

///////////////////////////////////////////////////////////////////////////////////////////////////
// UIScrollView

- (void)setContentSize:(CGSize)size {
if (_contentOrigin) {
CGFloat minHeight = self.height + _contentOrigin;
if (size.height < minHeight) {
size.height = self.height + _contentOrigin;
}
}

CGFloat y = self.contentOffset.y;
[super setContentSize:size];

if (_contentOrigin) {
// As described below in setContentOffset, UITableView insists on messing with the
// content offset sometimes when you change the content size or the height of the table
self.contentOffset = CGPointMake(0, y);
}
}

- (void)setContentOffset:(CGPoint)point {
// UITableView (and UIScrollView) are really stupid about resetting the content offset
// when the table view itself is resized. There are times when I scroll to a point and then
// disable scrolling, and I don't want the table view scrolling somewhere else just because
// it was resized.
if (self.scrollEnabled) {
if (!(_contentOrigin && self.contentOffset.y == _contentOrigin && point.y == 0)) {
[super setContentOffset:point];
}
}
}

只需评论它们,一切都会正常进行。我不知道这是否会破坏某个地方的某些东西,但至少,你知道问题出在哪里。

关于iphone - 如何阻止 UITableView moveRowAt IndexPath 在重新排序时留下空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2242387/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com