0

I have a table view that displays list of clients (logo of a client + name etc) and at the same time I created 4 buttons that link to specific information about each client (such as overview and such).

Everything works properly when clicking on these buttons and I am taken to the proper corresponding viewui info.

Now the problem is when I sort everything alphabetically. Everything gets sorted properly (company names and such) except the buttons still link to the previous indexPath.row order... So when I click on client one in the alpha list I see the information of the first client which is correct,., now if I click on the first client under the letter B... I see the information of first client in list A... so the indexpath of the buttons is not sorted at all....

Here is my code:

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

NSIndexPath *offsetPath = [self modelRowIndexForIndexPath:indexPath];
        //NSLog(@"Requested: %@  --  Off: %@",indexPath,offsetPath);



    RMCustomClientCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClientCell"];

    RMClients *client;

    if(!self.alphaMode){


        client = [self.clients objectAtIndex:indexPath.row];

    } else {

        NSString *key = [self.alphaKeys objectAtIndex:offsetPath.section];
        NSArray *specificClients = [self.sortedClients objectForKey:key];
        client = [specificClients objectAtIndex:offsetPath.row];


    }


cell.extraDetailsButton.tag = indexPath.row;
cell.overviewDetailsButton.tag = indexPath.row;
cell.listDetailsButton.tag = indexPath.row;
cell.eventsDetailsButton.tag = indexPath.row;


    cell.nameLabel.text = client.name;


    return cell;

}

Now how do I correct this cell.extraDetailsButton.tag = indexPath.row; (the indexPath.row) should be replaced by something else, right?

4

2 回答 2

0

对每个进行偏移等cellForRowAtIndexPath都是低效的并且容易出错(正如您所发现的那样)。相反,当您更改 tableview 上的排序方法时,请更改 self.clients 的排序,以便您的索引始终与您的 tables 匹配。如果你这样做,你可以简化你的按钮逻辑,如下所示:

为您的按钮定义 4 个标签。

#define BUTTON0_TAG 100
#define BUTTON1_TAG 101
#define BUTTON2_TAG 102
#define BUTTON3_TAG 103

将您的按钮绑定到此方法:

- (void)buttonAction:(UIButton *)sender {
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    switch (sender.tag) {
       case BUTTON0_TAG:
          [self detailsForClientAtIndexPath:indexPath];
          break;
       case BUTTON1_TAG:
          [self overviewForClientAtIndexPath:indexPath];
          break;
       case BUTTON2_TAG:
          [self listForClientAtIndexPath:indexPath];
          break;
       case BUTTON3_TAG:
          [self eventsForClientAtIndexPath:indexPath];
          break;
    }
}
于 2013-03-13T20:58:30.443 回答
0

Shouldn't it be set to offsetPath.row instead? Isn't that the index you're referring to in order to get the client object?

于 2013-03-13T20:43:53.393 回答