Ios 如何在cellForRowAtIndexPath中添加额外的单元格?
例如,我有3篇文章,当我显示文章时,我希望在第一个单元格之前再显示一个单元格(总共4个) 我需要显示第一篇不在数组中的文章,然后显示在数组中的文章 更新 我尝试了下一步:Ios 如何在cellForRowAtIndexPath中添加额外的单元格?,ios,uitableview,Ios,Uitableview,例如,我有3篇文章,当我显示文章时,我希望在第一个单元格之前再显示一个单元格(总共4个) 我需要显示第一篇不在数组中的文章,然后显示在数组中的文章 更新 我尝试了下一步: - (NSInteger)tableView:( UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ([arr count] + 1); } 但我的应用程序有时会崩溃,我通过NSLOG查看,然后在我调用[table
- (NSInteger)tableView:(
UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ([arr count] + 1);
}
但我的应用程序有时会崩溃,我通过NSLOG查看,然后在我调用[tableView reloadData]之前,该应用程序进入cellForRowAtIndexPath。是否将所有文章都保存在一个数组中?您也应该将新项目添加到数组中。数组是您的数据源。我认为,如果希望新项目出现在顶部单元格中,您可能希望将其作为数组中的第一个元素插入。一旦更新了数组,就应该调用
[mytableView reloadData]
,这将触发要调用的所有数据源方法并重新加载表的数据 您是否将所有文章保存在一个数组中?您也应该将新项目添加到数组中。数组是您的数据源。我认为,如果希望新项目出现在顶部单元格中,您可能希望将其作为数组中的第一个元素插入。一旦更新了数组,就应该调用[mytableView reloadData]
,这将触发要调用的所有数据源方法并重新加载表的数据 这是你不应该做的事
您可以通过返回一个视图(从-tableView:cellforrowatinexpath:)来欺骗框架,该视图将包含两个子视图:您的“第一篇文章”单元格和原始的第一个单元格;不要忘记修改-tableView:heightForCellAtIndexPath:(否则您的视图将被剪切)
但一般来说,您应该将表视图后面的数据模型更改为dispay 4 cells–这只是一种更有效的方法。这是您不应该真正做的事情 您可以通过返回一个视图(从-tableView:cellforrowatinexpath:)来欺骗框架,该视图将包含两个子视图:您的“第一篇文章”单元格和原始的第一个单元格;不要忘记修改-tableView:heightForCellAtIndexPath:(否则您的视图将被剪切) 但一般来说,您应该将表视图后面的数据模型更改为dispay 4单元格–这只是一种更有效的方法。您可以这样做: 使用此方法返回附加行:
// Each row array object contains the members for that section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [YouArray count]+1;
}
在结尾检查此添加行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a cell if one is not already available
UITableViewCell *cell = [self.mContactsTable dequeueReusableCellWithIdentifier:@"any-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"any-cell"] autorelease];
}
//Identify the added row
if(indexpath.row==0)
{
NSLog(@"This is first row");
}
else{
// Write your existing code
}
}
您可以这样做:
使用此方法返回附加行:
// Each row array object contains the members for that section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [YouArray count]+1;
}
在结尾检查此添加行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a cell if one is not already available
UITableViewCell *cell = [self.mContactsTable dequeueReusableCellWithIdentifier:@"any-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"any-cell"] autorelease];
}
//Identify the added row
if(indexpath.row==0)
{
NSLog(@"This is first row");
}
else{
// Write your existing code
}
}
您需要使用
insertObject
[arr insertObject:[NSNull null] atIndex:0];
并实施以下方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([arr onjectAtIndex:indexPath.row] == [NSNull null])
{
// 1st cell extra cell do your stuff
}
return cell;
}
您需要使用
insertObject
[arr insertObject:[NSNull null] atIndex:0];
并实施以下方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([arr onjectAtIndex:indexPath.row] == [NSNull null])
{
// 1st cell extra cell do your stuff
}
return cell;
}
是的,我把所有3篇文章都放在数组中。我更新了我的第一篇文章。是的,我保留了所有3篇文章。我更新了我的第一篇文章。我在第一篇文章(更新)中写道,当我这样做时会发生什么。@crios当应用程序崩溃时,控制台会打印什么?什么都没有。表get为空,仅此而已。那么为什么不检查对象是否为null,即[NSNull null]或数组计数?还要检查对象数组是否仍然有对象,或者您可能正在从数组中删除对象。还要在重新加载tableView的位置放置一些代码。我在第一篇文章(更新)中写道,当我这样做时会发生什么。@crios当应用程序崩溃时,控制台会打印什么?什么都没有。表get为空,仅此而已。那么为什么不检查对象是否为null,即[NSNull null]或数组计数?还要检查数组是否仍有对象,或者您可能正在从中删除对象。还要在重新加载tableView的位置放置一些代码。