我在网上浏览了所有的例子,无法弄清楚如何正确地将一个单元格添加到带动画的tableview中.假设我有一个部分有一个单元格,我想在用户点击第一个单元格的附件后添加另一个单元格.
我的"添加"方法执行此操作:
- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender {
if (switch1.on) {
NSArray *appleComputers = [NSArray arrayWithObjects:@"WWWWW" ,@"XXXX", @"YYYY", @"ZZZZ", nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"];
[listOfItems replaceObjectAtIndex:0 withObject:appleComputersDict];
[tblSimpleTable reloadData];
}
Run Code Online (Sandbox Code Playgroud)
哪个有效,但没有动画.我知道为了添加动画,我需要使用insertRowsAtIndexPaths:withRowAnimation,所以我尝试了很多选项但是在执行insertRowsAtIndexPaths:withRowAnimation方法时它总是崩溃.
我最近的尝试是通过这样做:
- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender {
if (switch1.on) {
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0]; //ALSO TRIED WITH indexPathRow:0
NSArray *indexArray = [NSArray arrayWithObjects:path1,nil];
[tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight];
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我怎样才能轻松实现这一目标?我不明白这整个indexPathForRow的事情......我也不明白如何使用这种方法我可以为新单元格添加标签名称.请帮忙......谢谢!!
问题总结:当我在有很多细胞经常死机UITableView动画的高度时UITableViewCell从UITextView编辑它的文本.使用iOS 8自定义单元格.
长问题:我已经成功实现了所以我可以动态地使用iOS 8自定义单元格将文本输入到单元格UITextView中并更改单元格高度而不会失去焦点(firstReponder).但是,如果tableView太大(行太多),它会崩溃.这是我的堆栈跟踪:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x000000010b3b3d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010b9cbdeb objc_exception_throw + 48
2 CoreFoundation 0x000000010b274cc5 -[__NSArrayM insertObject:atIndex:] + 901
3 UIKit 0x0000000108b05439 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke1029 + 180
4 UIKit 0x0000000108a7e838 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 582
5 UIKit 0x0000000108a7ec6d +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 105
6 UIKit 0x0000000108b05048 -[UITableView _updateWithItems:updateSupport:] + 4590 …Run Code Online (Sandbox Code Playgroud) 我正在使用xcode 4.2和storyboard来创建一个iphone应用程序.
当我按下右上角的编辑按钮时,我想有删除现有行的选项,并在顶部看到额外的单元格(带有绿色的"+"图标),这样我就可以添加一个新的单元格.
我有一个viewDidLoad使用CoreData 在方法中填充的数组
我已启用设置按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
Run Code Online (Sandbox Code Playgroud)
并实施了该方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// removing a cell from my array and db here...
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// adding a cell to my array and db here...
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我需要在某个时刻添加单元格然后我可以编辑但是我不清楚在哪里,我无法在互联网上找到解释.