Objective-C:TableView Sections和indexPaths使用数组

时间:2015-09-22 00:33:58

标签: ios objective-c uitableview

我一直在努力解决这个问题的逻辑几个小时,希望有人可以帮助我。

我的应用程序有一系列联系人,这些联系人都在我的地址簿中,并且还使用了应用程序(self.societyContacts)。

我打破了那个数组并将它放在我的viewDidLoad中的部分:

if ([MFMessageComposeViewController canSendText])

{
[controller setRecipients:nil];
[controller setBody:@"Text."];

NSData *data = UIImageJPEGRepresentation(_imageView.image, 1);
[controller addAttachmentData:data typeIdentifier:@"image/jpeg" filename:@"Image.jpeg"];

[self presentViewController:controller animated:YES completion:NULL]; }

以下是我用于显示数据的所有TableView委托方法:

- (void)viewDidLoad {

    [super viewDidLoad];

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

    for (int i=0; i<self.societyContacts.count; i++) {
        [self.itsToDoChecked insertObject:@"FALSE" atIndex:i];
    }

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];

    [self.societyContacts sortUsingDescriptors:[NSArray arrayWithObject:sort]];
    self.sections = [[NSMutableDictionary alloc] init];

    BOOL found;

    // Loop through the books and create our keys
    for (NSDictionary *contact in self.societyContacts)
    {
        NSString *c = [[contact objectForKey:@"firstName"] substringToIndex:1];

        found = NO;

        for (NSString *str in [self.sections allKeys])
        {
            if ([str isEqualToString:c])
            {
                found = YES;
            }
        }

        if (!found)
        {
            [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
    }

    for (NSDictionary *contact in self.societyContacts)
    {
        [[self.sections objectForKey:[[contact objectForKey:@"firstName"] substringToIndex:1]] addObject:contact];
    }

    [self.contactsTable reloadData];

    // Do any additional setup after loading the view.
}

现在,这是问题所在。到目前为止,一切都很好。

每个联系单元都有一个用作复选框的按钮,因此我可以判断用户已经选择了哪些按钮。

在我的cellForRowAtIndexPath中,我使用以下代码来设置单元格。

#pragma mark UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.sections allKeys] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

现在按钮动作。我不确定如何从单元格中获取正确的索引路径行以传递给按钮操作,以便我可以正确处理它。现在,当用户上下滚动时,按钮会不断变化。

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

    static NSString *simpleTableIdentifier = @"SocietyContactCell";

    SocietyContactCell *cell = (SocietyContactCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SocietyContactCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    NSDictionary *contact = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    NSString *fName = [contact objectForKey:@"firstName"];
    NSString *lName = [contact objectForKey:@"lastName"];

    if (fName == nil && lName == nil) {

        NSString *label = [NSString stringWithFormat:@"%@", [contact valueForKey:@"phoneNumbers"]];
        cell.nameLabel.text = label;

    } else if (fName == nil) {

        NSString *label = [NSString stringWithFormat:@"%@", [contact valueForKey:@"lastName"]];

        cell.nameLabel.text = label;

    } else if (lName == nil) {

        NSString *label = [NSString stringWithFormat:@"%@", [contact valueForKey:@"firstName"]];

        cell.nameLabel.text = label;

    } else {

        NSString *label = [NSString stringWithFormat:@"%@ %@", [contact valueForKey:@"firstName"], [contact valueForKey:@"lastName"]];

        cell.nameLabel.text = label;

    }


    BOOL checked =  [[self.itsToDoChecked objectAtIndex:indexPath.row] boolValue];
    UIImage *image = (checked) ? [UIImage imageNamed:@"checkedContact.png"] : [UIImage imageNamed:@"uncheckedContact.png"];

    [cell.checkButton setBackgroundImage:image forState:UIControlStateNormal];

    [cell.checkButton setTag:indexPath.row];
    [cell.checkButton addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.checkButton addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];

    return cell;

}

如果有人对如何获取按钮标记的正确索引路径行有一些建议,那么我可以确保检查按钮正常运行并且我可以访问正确的联系人以满足其他编程需求,这将是很好的

1 个答案:

答案 0 :(得分:0)

您需要查看self.itsToDoChecked,因为您没有基于部分作为联系人,然后按钮将无法按预期工作。现在,一旦您更改了行上的按钮状态,其他具有相同行索引的部分上的所有按钮也会更改。 要解决此问题,您可以使用与保存联系人节点相同的模式来保存toDoChecked for section。 ...

self.sections = [[NSMutableDictionary alloc] init];
self.itsToDoCheckedInSections = [[NSMutableDictionary alloc] init];

...

for (NSDictionary *contact in self.societyContacts){

 [[self.sections objectForKey:[[contact objectForKey:@"firstName"] substringToIndex:1]] addObject:contact];

 [[self.itsToDoCheckedInSections objectForKey:[[contact objectForKey:@"firstName"] substringToIndex:1]] addObject:@"FALSE"];
}

...

在cellForRow中

NSDictionary *contact = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

BOOL *checked = [[[self.itsToDoCheckedInSections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] boolValue];

在buttonPress中

- (void)buttonPressedAction:(id)sender {

    UIButton *button = (UIButton *)sender;
    int row = button.tag;

//be sure of your view hierarchy perhaps you need more superview to get SocietyContactCell then the needed section
    SocietyContactCell *cell = (SocietyContactCell *)[button superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    NSLog(@"row: %i", row);
    NSLog(@"row: %i", row); 

    BOOL *checked = [[[self.itsToDoCheckedInSections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] boolValue];

...

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"uncheckedContact.png"] : [UIImage imageNamed:@"checkedContact.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

}