gpt4 book ai didi

ios - 无法在UITableView中删除indexpath

转载 作者:行者123 更新时间:2023-12-01 19:06:36 26 4
gpt4 key购买 nike

我有UITableView单元格用于显示UITextView文本。我需要使用UITableViewCellEditingStyleDelete删除一些单元格行文本。当我使用编辑按钮删除一些文本时,出现错误。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];

// NSLog(@"array is %@",myMutableArrayAgain);

return [myMutableArrayAgain count];

}

删除功能:
-(void)editButtonPressed:(UIButton *)button{

NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

[tableView setEditing:![tableView isEditing] animated:YES];

NSString *buttonTitle = ([tableView isEditing]) ? @"Done" : @"Edit";

[editButton setTitle:buttonTitle forState:UIControlStateNormal];

NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}


- (void)tableView:(UITableView *)tableView1
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);


NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];


if (editingStyle == UITableViewCellEditingStyleDelete)
{

//first delete this from the db of favorites table
NSLog(@"art id is %@",[myMutableArrayAgain objectAtIndex:indexPath.row]);





NSLog(@"indexpath is %d", indexPath.row);

[myMutableArrayAgain removeObjectAtIndex:indexPath.row];




NSLog(@"remove %d",indexPath.row);

NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];

NSLog(@"indextoremove %@",indexPathsToRemove);

[tableView1 deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];

}

NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}



- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];


NSString *contentsToMove = [[myMutableArrayAgain objectAtIndex:[fromIndexPath row]] retain];

[myMutableArrayAgain removeObjectAtIndex:[fromIndexPath row]];

[myMutableArrayAgain insertObject:contentsToMove atIndex:[toIndexPath row]];

[contentsToMove release];

NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

Nslog中的错误:
2013-10-07 14:29:41.939 WorkSa[3689:c07] indexpath is 0
2013-10-07 14:29:41.940 WorkSa[3689:c07] remove 0
2013-10-07 14:29:41.940 WorkSa[3689:c07] indextoremove (
"<NSIndexPath 0x8b44fa0> 2 indexes [0, 0]"
)
2013-10-07 14:29:41.941 WorkSa[3689:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
2013-10-07 14:29:41.942 WorkSafeACT[3689:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

最佳答案

删除后需要添加syncyize语句,如下所示更改功能。

- (void)tableView:(UITableView *)tableView1
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//first delete this from the db of favorites table
NSLog(@"art id is %@",[myMutableArrayAgain objectAtIndex:indexPath.row]);
NSLog(@"indexpath is %d", indexPath.row);
[myMutableArrayAgain removeObjectAtIndex:indexPath.row];

//Add these 2 lines - Similarly you will need to do in moving items.
[[NSUserDefaults standardUserDefaults] setObject:myMutableArrayAgain forKey:@"save"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Add these 2 lines

[myMutableArrayAgain removeObjectAtIndex:indexPath.row];
NSLog(@"remove %d",indexPath.row);
NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
NSLog(@"indextoremove %@",indexPathsToRemove);
[tableView1 deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];

}

NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}

您也可以使用以下代码重新定义NSLog,并仅打印变量或字符串,这些变量或字符串将为您提供方法名称及所有名称,并在appdelegate中的任何位置定义
#ifdef DEBUG
# define NLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif

Check this for more information on NSLog formatting

关于ios - 无法在UITableView中删除indexpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19220781/

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