1

我正在开发一个包含两个部分的动态表格视图:-注册签名-拒绝签名

我所有的值都在每个部分取决于字典中的标志:

[[self.userSignatures objectAtIndex:indexPath.row] objectForKey:@"idFirma"]

如果为0则表示未注册签名,否则表示已注册

但我不确定如何在我的方法中开发打印部分

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

这是我的发展:

#pragma mark *** Common methods: Tableview delegate methods ***
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0)   return @"not registered signatures";
    return @"Registered signatures";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.

    if ([self.userSignatures count] > 0){
        int firmasRegistradas = 0;
        for (int i = 0; i < [self.userSignatures count]; i++) {
            if (![[[self.userSignatures objectAtIndex:i] valueForKey:@"idFirma"] isEqualToString:@"0"])
                firmasRegistradas += 1;
        }
        if (section == 0)   return [self.userSignatures count] - firmasRegistradas;
        if (section == 1)   return firmasRegistradas;
    }    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    UILabel     *EntidadFirmante    = (UILabel *)   [cell viewWithTag:2];
    UILabel     *statusFirma        = (UILabel *)   [cell viewWithTag:3];

    if ([self.userSignatures count] > 0) {
        EntidadFirmante.text    = [[self.userSignatures objectAtIndex:indexPath.row]objectForKey:@"titFirmante"];
        statusFirma.text        = [[self.userSignatures objectAtIndex:indexPath.row]objectForKey:@"idDoc"];

        cell.accessoryType      = UITableViewCellAccessoryDisclosureIndicator;

        NSLog(@"idFirma: %@",[[self.userSignatures objectAtIndex:indexPath.row] objectForKey:@"idFirma"]);

        if (indexPath.section == 0 && [[[self.userSignatures objectAtIndex:indexPath.row] objectForKey:@"idFirma"] isEqualToString:@"0"]) return cell;
        else
        if (indexPath.section == 1 && ![[[self.userSignatures objectAtIndex:indexPath.row] objectForKey:@"idFirma"] isEqualToString:@"0"]) return cell;

    }
    else{
        EntidadFirmante.text    = @"There's no signatures";
        statusFirma.text        = @"without signatures";

        cell.accessoryType      = UITableViewCellAccessoryNone;
    }
    return cell;
}

关键在这里:

if (indexPath.section == 0 && [[[self.userSignatures objectAtIndex:indexPath.row] objectForKey:@"idFirma"] isEqualToString:@"0"]) return cell;
else
if (indexPath.section == 1 && ![[[self.userSignatures objectAtIndex:indexPath.row] objectForKey:@"idFirma"] isEqualToString:@"0"]) return cell;

如果我有这样的清单

  1. 挂号的
  2. 挂号的
  3. 未注册
  4. 挂号的

我期待这样的事情:

注册签名:1. 2. 4.

3.未注册的签名

但到目前为止我得到了这样的东西

注册签名:1. 2. 4.

未注册签名 1.(已注册)

如果未注册的签名是第三个,它会打印但第一个(已注册)

任何帮助我都会感激

4

2 回答 2

1

当部分更改时,它会重置 indexPath。您正确识别了行数,但由于您的已注册数据和未注册数据都在同一个数组中,因此它在未注册部分的索引 1 处显示数据。这就是为什么对于行数为 1 的部分,它会在数组的索引 1 处显示数据。

您可以通过多种方式解决此问题。您可以将数据分成注册数组和未注册数组。或者您可以识别未注册数据的正确索引并将其传递给 objectAtIndexPath 方法。

此外,恕我直言,如果您在方法中配置了您的单元格,您的代码会更简洁:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

}

代替cellForRowAtIndexPath:

于 2013-09-10T00:25:16.733 回答
0

最后,感谢 WeekendCodeWarrior 的建议,我使它工作,现在这是我的代码:

#pragma mark *** UITableviewDelegate ***
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    //Devuelve el título que tendrá cada sección
    if (section == 0)   return @"not registered signatures";
    return @"Registered signatures";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    registeredSignatures    = [[NSMutableArray alloc]init];
    unregisteredSignatures  = [[NSMutableArray alloc]init];

    for (int i = 0; i < [self.userSignatures count]; i++) {
        if ([[[self.userSignatures objectAtIndex:i] valueForKey:@"idFirma"] isEqualToString:@"0"])
            [unregisteredSignatures addObject:[self.userSignatures objectAtIndex:i]];
        else
        if (![[[self.userSignatures objectAtIndex:i] valueForKey:@"idFirma"] isEqualToString:@"0"])
                [registeredSignatures addObject:[self.userSignatures objectAtIndex:i]];
    }


    if (section == 0)   return [unregisteredSignatures count];
    if (section == 1)   return [registeredSignatures count];
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    UILabel     *EntidadFirmante    = (UILabel *)   [cell viewWithTag:2];
    UILabel     *statusFirma        = (UILabel *)   [cell viewWithTag:3];

    if ([self.userSignatures count] > 0) {
        cell.accessoryType      = UITableViewCellAccessoryDisclosureIndicator;

        if (indexPath.section == 0) {
            EntidadFirmante.text    = [[unregisteredSignatures objectAtIndex:indexPath.row]objectForKey:@"titFirmante"];
            statusFirma.text        = [[unregisteredSignatures objectAtIndex:indexPath.row]objectForKey:@"idDoc"];    
        }

        else
            if (indexPath.section == 1) {
                EntidadFirmante.text    = [[registeredSignatures objectAtIndex:indexPath.row]objectForKey:@"titFirmante"];
                statusFirma.text        = [[registeredSignatures objectAtIndex:indexPath.row]objectForKey:@"idDoc"];
            }
    }
    else{
        EntidadFirmante.text    = @"There's no signatures";
        statusFirma.text        = @"without signatures";

        cell.accessoryType      = UITableViewCellAccessoryNone;
    }
    return cell;
}

感谢您的支持!!!

于 2013-09-10T16:44:12.127 回答