Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何正确使用InsertRowsatindExpath?_Iphone_Objective C_Xcode_Uitableview - Fatal编程技术网

Iphone 如何正确使用InsertRowsatindExpath?

Iphone 如何正确使用InsertRowsatindExpath?,iphone,objective-c,xcode,uitableview,Iphone,Objective C,Xcode,Uitableview,我在网上浏览了所有的示例,但不知道如何正确地将单元格添加到带有动画的tableview中。假设我有一个部分有一个单元格,我想在用户单击第一个单元格的附件后添加另一个单元格 我的“添加”方法执行以下操作: - (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender { if (switch1.on) { NSArray *appleComputers = [NSArray arrayWithObjects:@"WW

我在网上浏览了所有的示例,但不知道如何正确地将单元格添加到带有动画的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];

}
这是工作,但没有动画。我知道为了添加动画,我需要使用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];

}
}  

我做错了什么?我怎样才能轻松做到这一点?我不明白整个indexPathForRow的事情…我也不明白如何用这个方法向新单元格添加标签名。请帮忙…谢谢

首先,您应该在更新表本身之前更新数据模型。 您还可以使用:

[tableView beginUpdates];
// do all row insertion/delete here
[tableView endUpdates];

并且表将使用动画一次生成所有更改的内容(如果您指定它)

使用
insertRowsAtIndexPaths
时要记住的重要一点是,您的
UITableViewDataSource
需要匹配insert告诉它要执行的操作。如果向tableview添加行,请确保备份数据已更新为匹配。

这是一个两步过程:

首先更新数据源,以便
numberofrowsinssection
cellforrowatinexpath
将为插入后的数据返回正确的值。必须在插入或删除行之前执行此操作,否则将看到出现“无效行数”错误

然后插入您的行:

[tblSimpleTable beginUpdates];
[tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight];
[tblSimpleTable endUpdates];
简单地插入或删除一行不会改变数据源;您必须自己做。

对于swift用户

// have inserted new item into data source

// update
self.tableView.beginUpdates()
var ip = NSIndexPath(forRow:find(self.yourDataSource, theNewObject)!, inSection: 0)
self.tableView.insertRowsAtIndexPaths([ip], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.endUpdates()

insertRowsAtIndexPaths:withRowAnimation:
和对数据模型的更改都需要在开始更新和结束更新之间进行

我已经创建了一个简单的示例,它应该能够独立工作。由于找不到任何简单的例子,我花了一个星期的时间试图弄明白这一点,所以我希望这能节省一些人的时间和头痛

@interface MyTableViewController ()
@property (nonatomic, strong) NSMutableArray *expandableArray;
@property (nonatomic, strong) NSMutableArray *indexPaths;
@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation MyTableViewController

- (void)viewDidLoad
{
    [self setupArray];
}

- (void)setupArray
{
    self.expandableArray = @[@"One", @"Two", @"Three", @"Four", @"Five"].mutableCopy;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.expandableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you should create a cell that displays information from self.expandableArray, and return it
}

//call this method if your button/cell/whatever is tapped
- (void)didTapTriggerToChangeTableView
{
    if (/*some condition occurs that makes you want to expand the tableView*/) {
        [self expandArray]
    }else if (/*some other condition occurs that makes you want to retract the tableView*/){
        [self retractArray]
    }
}

//this example adds 1 item
- (void)expandArray
{
    //create an array of indexPaths
    self.indexPaths = [[NSMutableArray alloc] init];
    for (int i = theFirstIndexWhereYouWantToInsertYourAdditionalCells; i < theTotalNumberOfAdditionalCellsToInsert + theFirstIndexWhereYouWantToInsertYourAdditionalCells; i++) {
        [self.indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }

    //modify your array AND call insertRowsAtIndexPaths:withRowAnimation: INBETWEEN beginUpdates and endUpdates
    [self.myTableView beginUpdates];
    //HERE IS WHERE YOU NEED TO ALTER self.expandableArray to have the additional/new data values, eg:
    [self.expandableArray addObject:@"Six"];
    [self.myTableView insertRowsAtIndexPaths:self.indexPaths withRowAnimation:(UITableViewRowAnimationFade)];  //or a rowAnimation of your choice

    [self.myTableView endUpdates];
}

//this example removes all but the first 3 items
- (void)retractArray
{
    NSRange range;
    range.location = 3;
    range.length = self.expandableArray.count - 3;

    //modify your array AND call insertRowsAtIndexPaths:withRowAnimation: INBETWEEN beginUpdates and endUpdates
    [self.myTableView beginUpdates];
    [self.expandableArray removeObjectsInRange:range];
    [self.myTableView deleteRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationFade];  //or a rowAnimation of your choice
    [self.myTableView endUpdates];
}

@end
@接口MyTableViewController()
@属性(非原子,强)NSMutableArray*expandableArray;
@属性(非原子,强)NSMutableArray*索引路径;
@属性(非原子,强)UITableView*myTableView;
@结束
@MyTableViewController的实现
-(无效)viewDidLoad
{
[自设置阵列];
}
-(void)设置数组
{
self.expandableArray=@[@“一”,“二”,“三”,“四”,“五]。mutableCopy;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回self.expandableArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//在这里,您应该创建一个单元格,显示self.expandableArray中的信息,并返回它
}
//如果点击了按钮/单元格/任何内容,请调用此方法
-(void)didTapTriggerToChangeTableView
{
如果(/*出现某种情况,使您希望展开tableView*/){
[自扩展阵列]
}否则,如果(/*发生其他一些情况,使您想收回tableView*/){
[自伸缩阵列]
}
}
//此示例添加了1项
-(void)扩展数组
{
//创建一个索引路径数组
self.indexPaths=[[NSMutableArray alloc]init];
对于(int i=要插入附加单元格的第一个索引;i<要插入的附加单元格总数+要插入附加单元格的第一个索引;i++){
[self.indexPaths addObject:[NSIndexPath indexPathForRow:i第0节];
}
//修改数组并在BeginUpdate和EndUpdate之间调用insertRowsAtIndexPaths:withRowAnimation:InBeginUpdate
[self.myTableView开始更新];
//这里是您需要更改self.expandableArray以获得附加/新数据值的地方,例如:
[self.expandableArray addObject:@“六个”];
[self.myTableView insertRowsAtIndexPaths:self.InExpaths with rowAnimation:(UITableViewRowAnimationFade)];//或您选择的rowAnimation
[self.myTableView endUpdates];
}
//本例删除除前3项外的所有项
-(void)数组
{
NSRange范围;
range.location=3;
range.length=self.expandableArray.count-3;
//修改数组并在BeginUpdate和EndUpdate之间调用insertRowsAtIndexPaths:withRowAnimation:InBeginUpdate
[self.myTableView开始更新];
[self.expandableArray removeObjectsInRange:range];
[self.myTableView deleteRowsAtIndexPaths:self.indexPaths with rowAnimation:UITableViewRowAnimationFade];//或您选择的rowAnimation
[self.myTableView endUpdates];
}
@结束

您是否使用2项更新了数据源?我认为您在更改之前没有更新数据模型。您可以发布异常消息吗?2011-08-04 10:54:55.601 ViewTest[36300:b903]***在-[UITableView\u EndcellAnimationWithContext:],/SourceCache/UIKit\u Sim/UIKit-1448.89/UITableView.m:995 2011-08-04 10:54:55.602 ViewTest[36300:b903]CoreAnimation:忽略异常:无效更新:第0节中的行数无效。更新(1)后现有节中包含的行数必须等于更新(1)前该节中包含的行数,加上或减去从该节中插入或删除的行数(插入1,删除0)。在[tableView EndUpdate]期间崩溃;您知道,调试器告诉您第0节中有多少行不响应更新后的行数(取决于您正在执行的插入/删除操作)。数据源有问题,可能是在更新块期间更改了,或者根本没有更改以对应新状态。那么我的索引呢?假设我在一行表中再添加一行,它们是否正确?好的,您是对的,我删除了所有索引