我正在尝试使用以下选项进行UITableView(分组):
Section 1:
TextField for name of something
Label + stepper for increasing the Sections where ppl can add options (Subclassed UITableViewCell)
Section 2: (Default is this section, that they already can add things)
First cell of section is an cell (Add Option). People click this to get an extra cell in section 2 where they can get things going. When they click this, an UIAlertView will show up with a textfield.
Section 3:
Same as section 2 from now on.
所以我已经有了这个设置,但是现在我试图将“ Add Option”添加到带有Arrays的部分。因为添加选项始终是该节的最后一个索引路径。因此将标识符链接到它。然后,当我初始化应用程序时,默认数组将如下所示:
__diceArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObject:@"THIS IS FOR SECTION ONE"], [NSArray arrayWithObjects:@"Option in section 2", @"Option for section 2", nil], nil];
但是现在我在尝试将内容添加到第2部分时遇到了一些问题,例如我无法从第2部分将数组添加到我的数组中。当我尝试重新加载tableview时,它崩溃了。
有人有更好的方法吗?
编辑:
忘记了一些东西,它们是:
我正在像这样更新数组。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
[[self _diceArray] insertObject:name atIndex:2];
NSLog(@"%@", self._diceArray);
[[self _tabel]reloadData];
}
}
插入一个对象,但不插入该部分的数组中:
(
(
“” //这是第一节中的用户配置
),
(
测试,//需要在这里。
测试//这最后一个用于添加Option原型单元。
),
adsf //这是添加的对象。
)
同样,重载数据崩溃:
*** First throw call stack:
(
0 CoreFoundation 0x0000000101bb1495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001016b199e objc_exception_throw + 43
2 CoreFoundation 0x0000000101c4265d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000101ba2d8d ___forwarding___ + 973
4 CoreFoundation 0x0000000101ba2938 _CF_forwarding_prep_0 + 120
5 multiDice 0x0000000100003752 -[AddViewController tableView:numberOfRowsInSection:] + 146
6 UIKit 0x0000000100471e1e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2245
7 UIKit 0x00000001004751c2 -[UITableViewRowData numberOfRows] + 97
8 UIKit 0x000000010032323c -[UITableView noteNumberOfRowsChanged] + 114
9 UIKit 0x0000000100322d27 -[UITableView reloadData] + 717
10 multiDice 0x00000001000042dc -[AddViewController alertView:clickedButtonAtIndex:] + 332
11 UIKit 0x0000000100785ec8 -[_UIModalItemsCoordinator _notifyDelegateModalItem:tappedButtonAtIndex:] + 151
12 UIKit 0x000000010034c53e -[_UIModalItemAlertContentView tableView:didSelectRowAtIndexPath:] + 364
13 UIKit 0x00000001003265c2 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1312
14 UIKit 0x00000001003266eb -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 221
15 UIKit 0x0000000100277100 _applyBlockToCFArrayCopiedToStack + 316
16 UIKit 0x0000000100276f71 _afterCACommitHandler + 460
17 CoreFoundation 0x0000000101b7cdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18 CoreFoundation 0x0000000101b7cd37 __CFRunLoopDoObservers + 391
19 CoreFoundation 0x0000000101b5c522 __CFRunLoopRun + 946
20 CoreFoundation 0x0000000101b5bd83 CFRunLoopRunSpecific + 467
21 GraphicsServices 0x0000000103d28f04 GSEventRunModal + 161
22 UIKit 0x000000010025ee33 UIApplicationMain + 1010
23 multiDice 0x0000000100002c73 main + 115
24 libdyld.dylib 0x00000001022495fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
亲切的问候!
最佳答案
更新:
您用2个数组初始化数组
__diceArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObject:@"THIS IS FOR SECTION ONE"],
这段代码会在您初始化了2个数组后将字符串添加到您的NSMutableArray中
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
[[self _diceArray] insertObject:name atIndex:2];
因此,当它要求numberOfRowsInSection时,当它到达第三个条目时,它的NSString显然不会响应count。
我不确定您要尝试使用Alertview中的添加实现什么,您是要添加到现有部分还是新部分?无论哪种方式,您都需要记住要添加到该部分的部分,握住该部分内的NSMutableArray并插入对象。但是您将需要像我一开始所说的那样更改__diceArray的初始化。
如果我们要为您提供帮助,您需要帮助我们,例如,您遇到了什么错误,但是作为开始,如果您试图为每个部分的数组添加元素,则它们也必须为
NSMutableArray
__Array = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObject:@"THIS IS FOR SECTION ONE"], [NSMutableArray arrayWithObjects:@"Option in section 2", @"Option for section 2", nil], nil];
但是,正如我所说的,您需要告诉我们错误时,了解您如何尝试更新__Array可能也很有用。