草庐IT

ios - 无效更新 : invalid number of rows in section 0. 更新后现有部分中包含的行数 (3)

coder 2024-01-23 原文

任何人都可以帮助我,我是 iOS 开发的新手,我在代码中做错了什么。每当我点击该行时,它应该在其下方插入另一行。

这是我的代码

错误: 原因:'无效更新:第 0 部分中的行数无效。更新 (3) 后现有部分中包含的行数必须等于该数字更新前该部分中包含的行数 (3)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    [self setupViewController];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
    [self setupViewController];
}
return self;
}



- (void)setupViewController
{
NSArray* colors = [[NSArray alloc]initWithObjects:@"PENDENTS",@"EARRINGS",@"PENDENT SETS", nil];

self.data = [[NSMutableArray alloc] init];

for (int i = 0 ; i < [colors count] ; i++)
{
    NSMutableArray* section = [[NSMutableArray alloc] init];

    for (int j = 0 ; j < 1 ; j++)
    {
        [section addObject:[NSString stringWithFormat:@"EAR RINGS  ", j]];
        [section addObject:[NSString stringWithFormat:@"PENDENT SETS ",j]];
        [section addObject:[NSString stringWithFormat:@"PENDENTS ",j]];
        [section addObject:[NSString stringWithFormat:@"25 pieces ",j]];
        [section addObject:[NSString stringWithFormat:@"329.672 ",j]];
        [section addObject:[NSString stringWithFormat:@"may 1 ",j]];
        [section addObject:[NSString stringWithFormat:@"may 10 ",j]];
        [section addObject:[NSString stringWithFormat:@"8,40,000 ",j]];
    }

    [self.data addObject:section];
}

self.headers = [[NSMutableArray alloc] init];

for (int i = 0 ; i < [colors count] ; i++)
{

    UIView* header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 40)];
    UILabel *lblpend=[[UILabel alloc]initWithFrame:CGRectMake(12, 0, 170, 40)];
    UILabel *lbldeliver=[[UILabel alloc]initWithFrame:CGRectMake(332, 0,113,40)];
    UILabel *lbltotal =[[UILabel alloc]initWithFrame:CGRectMake(562, 0, 200, 40)];

    [lblpend setText:@"RM-15/16-GRT-0102-CHE"];
    [lbldeliver setText:@"Delivered:27 Mar"];
    [lbltotal setText:@"941.92gms  Rs24,00,000"];

    [lbltotal setFont:[UIFont systemFontOfSize:14]];
    [lbldeliver setFont:[UIFont systemFontOfSize:14]];
    [lblpend setFont:[UIFont systemFontOfSize:14]];

    [header addSubview:lblpend];
    [header addSubview:lbldeliver];
    [header addSubview:lbltotal];
    [header setBackgroundColor:[UIColor greenColor]];
    [self.headers addObject:header];
}
}


- (void)viewDidLoad {

[super viewDidLoad];

self.expandablecells=[[NSMutableArray alloc]initWithObjects:@"cozycoracle",@"pendents",@"pendent sets", nil];

isTapped=YES;

[self.tbldata reloadData];
[self.tbldata openSection:0 animated:NO];
[super viewDidLoad];
// Do any additional setup after loading the view.
}

pragma tableview delegates

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isTapped==YES) {

    isTapped=NO;

    return [self.expandablecells count];
}
return [self.data count];


}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (isTapped==YES) {

    isTapped=NO;

    return [self.expandablecells count];

}

return [self.data count];


}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"cell";

MyOrderCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell)
{
    cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}


if (isTapped==NO) {

    //isTapped=YES;

    NSString* text = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell.lblpendnts.text = text;
    cell.lblpieces.text=text;
    cell.lblnum.text=text;
    cell.lbldate.text=text;
    cell.lblenddate.text=text;
    cell.lbltotl.text=text;



}else{

    cell.lblpendnts.text = [self.expandablecells objectAtIndex:indexPath.row];
    cell.lblpieces.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lblnum.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lbldate.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lblenddate.text=[self.expandablecells objectAtIndex:indexPath.row];
    cell.lbltotl.text=[self.expandablecells objectAtIndex:indexPath.row];

}


return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

MyOrderCell *cell=[[MyOrderCell alloc]init];

if (isTapped==NO) {

    //isTapped=NO;

    if (indexPath.section == 0 & indexPath.row==0) {
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
        indexPath = newPath;

        [[self tbldata] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];


    }

}

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [self.headers objectAtIndex:section];
}

最佳答案

你有这个问题是因为你需要在调用时更改数据源(为新行添加一个对象)

[[self tbldata] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

详见 docs

关于ios - 无效更新 : invalid number of rows in section 0. 更新后现有部分中包含的行数 (3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38092379/

有关ios - 无效更新 : invalid number of rows in section 0. 更新后现有部分中包含的行数 (3)的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - 在 heroku 的 .fonts 文件夹中包含自定义字体,似乎无法识别它们 - 2

    Heroku支持人员告诉我,为了在我的Web应用程序中使用自定义字体(未安装在系统中,您可以在bash控制台中使用fc-list查看已安装的字体)我必须部署一个包含所有字体的.fonts文件夹里面的字体。问题是我不知道该怎么做。我的意思是,我不知道文件名是否必须遵循heroku的任何特殊模式,或者我必须在我的代码中做一些事情来考虑这种字体,或者如果我将它包含在文件夹中它是自动的......事实是,我尝试以不同的方式更改字体的文件名,但根本没有使用该字体。为了提供更多详细信息,我们使用字体的过程是将PDF转换为图像,更具体地说,使用rghostgem。并且最终图像根本不使用自定义字体。在

  8. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  9. ruby-on-rails - Rails Associations 的更新方法是什么? - 2

    这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user

  10. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

随机推荐