如何根据objective-C中的标志在动态表视图部分中添加单元格

时间:2021-10-26 22:21:44

I'm developing a dynamic tableview with two sections: -Registered signatures -Rejected signatures

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

all my values go on each section depending of a flag inside a dictionary:

我所有的价值观都是根据字典里的国旗来进行的:

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

if it comes in 0 it means not registered signature, otherwise it means registered

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

but I'm not sure how to develop the section of printing in my method

但是我不知道如何在我的方法中发展印刷这一部分

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

this is my development:

这是我的发展:

#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;
}

the key is here:

这里的关键是:

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;

if I have a list like this one

如果我有一个这样的列表

  1. Registered
  2. 注册
  3. Registered
  4. 注册
  5. Unregistered
  6. 未注册的
  7. Registered
  8. 注册

I expect something like this:

我期望的是:

Registered Signatures: 1. 2. 4.

注册签名:1。2。4所示。

Unregistered signatures 3.

未注册的签名3。

but so far I get something like this

但到目前为止,我得到了这样的结果

Registered Signatures: 1. 2. 4.

注册签名:1。2。4所示。

Unregistered signatures 1. (registered)

未注册的签名1。(注册)

if the unregisteded signature is the third one, it prints but the first one (registered)

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

any help I'll appreciate

任何帮助我欣赏

2 个解决方案

#1


1  

When sections change it resets indexPath. You are correctly identifying the number of rows but since both your registered and unregistered data are in the same array it is displaying the data at index 1 in the unregistered section. Thats why for a section with a row count of 1 it displays the data at index 1 of your array.

当区段更改时,它重新设置indexPath。您正在正确地标识行数,但是由于您的已注册数据和未注册数据都位于同一个数组中,所以它将在未注册部分的索引1中显示数据。这就是为什么对于行数为1的部分,它在数组的索引1处显示数据。

You could fix this a number of ways. You could separate your data into a registered array and into an unregistered array. Or you can identify the correct index of the unregistered data and pass that to the objectAtIndexPath method.

你可以用很多方法来解决这个问题。可以将数据分离到已注册数组和未注册数组中。或者可以标识未注册数据的正确索引,并将其传递给objectAtIndexPath方法。

Also, IMHO your code would be a lot cleaner if you configured your cell in the method:

此外,如果您在方法中配置单元格,您的代码将更加清晰:

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

}

}

instead of cellForRowAtIndexPath:

而不是cellForRowAtIndexPath:

#2


0  

Finally I make it work thanks to WeekendCodeWarrior suggestions, now this is my code:

最后,多亏了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;
}

thanks for the support!!!

谢谢你的支持! ! !

#1


1  

When sections change it resets indexPath. You are correctly identifying the number of rows but since both your registered and unregistered data are in the same array it is displaying the data at index 1 in the unregistered section. Thats why for a section with a row count of 1 it displays the data at index 1 of your array.

当区段更改时,它重新设置indexPath。您正在正确地标识行数,但是由于您的已注册数据和未注册数据都位于同一个数组中,所以它将在未注册部分的索引1中显示数据。这就是为什么对于行数为1的部分,它在数组的索引1处显示数据。

You could fix this a number of ways. You could separate your data into a registered array and into an unregistered array. Or you can identify the correct index of the unregistered data and pass that to the objectAtIndexPath method.

你可以用很多方法来解决这个问题。可以将数据分离到已注册数组和未注册数组中。或者可以标识未注册数据的正确索引,并将其传递给objectAtIndexPath方法。

Also, IMHO your code would be a lot cleaner if you configured your cell in the method:

此外,如果您在方法中配置单元格,您的代码将更加清晰:

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

}

}

instead of cellForRowAtIndexPath:

而不是cellForRowAtIndexPath:

#2


0  

Finally I make it work thanks to WeekendCodeWarrior suggestions, now this is my code:

最后,多亏了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;
}

thanks for the support!!!

谢谢你的支持! ! !