首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableArray / UITableView

NSMutableArray / UITableView
EN

Stack Overflow用户
提问于 2014-03-30 09:49:23
回答 2查看 124关注 0票数 0

我正在尝试使用具有以下选项的UITableView (分组):

代码语言:javascript
复制
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选项“。因为add选项始终是节的最后一个索引路径。所以把标识符和它联系起来。然后,当我初始化应用程序时,默认数组将如下所示:

代码语言:javascript
复制
__diceArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObject:@"THIS IS FOR SECTION ONE"], [NSArray arrayWithObjects:@"Option in section 2", @"Option for section 2", nil], nil];

但是现在,当我试图在我的第2节中添加一些东西时,我遇到了一些问题,比如我无法从第2节将数组放入我的数组中,当我试图重新加载表视图时,它会崩溃。

有人有更好的方法吗?

编辑:

忘记了一些事情,它们就在这里:

我正在像这样更新数组。

代码语言:javascript
复制
- (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];
    }
}

插入一个对象,但不在节的数组中:(( "“//这是用于第一节中的用户配置),( Test,//需要在这里)。测试//最后一个是单元格。),这是添加的对象。)

此外,重新加载的数据崩溃:

代码语言:javascript
复制
*** 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)

亲切的问候!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-30 09:54:44

更新:

使用2个数组初始化数组

代码语言:javascript
复制
__diceArray = [NSMutableArray arrayWithObjects:[NSArray arrayWithObject:@"THIS IS FOR SECTION ONE"], 

这段代码将在您使用2个数组初始化NSMutableArray之后,将字符串添加到它中。

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        [[self _diceArray] insertObject:name atIndex:2];

因此,当它请求numberOfRowsInSection时,当它到达第三个条目时,它是一个明显不响应计数的NSString。

我不知道您试图从警报视图中添加什么,您是试图添加到一个现有的部分还是一个新的部分?无论哪种方式,您都需要记住要将其添加到的部分,获取该部分中的NSMutableArray并插入对象。但是,正如我在开始时所说的,你需要改变你对__diceArray的初始化.

如果我们要帮助您,您需要帮助我们,例如,您得到了什么错误,但首先,如果您试图为每个部分添加元素,那么它们也需要是NSMutableArray

代码语言:javascript
复制
__Array = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObject:@"THIS IS FOR SECTION ONE"], [NSMutableArray arrayWithObjects:@"Option in section 2", @"Option for section 2", nil], nil];

但是,正如我所说的,您需要告诉我们错误,看看您是如何更新__Array的,这也是有用的。

票数 1
EN

Stack Overflow用户

发布于 2015-06-19 17:21:50

每当您向/从https://github.com/xmartlabs/XLData插入/移除项节时,UITableView都会自动更新XLDataSet

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22742544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档