0

我正在制作一个显示用户联系人列表和建议联系人的应用程序。我希望像这样预先选择建议的联系人部分的复选框。有人帮我吗?

![1]: http://min.us/mOhamWjnd

到目前为止,我不知道如何实现这一点。当我尝试时,所有复选框都被预先选中。

这是我的 cellforRowIndexPath 代码

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AddressBookCell *cell = nil;
if([tableView isEqual:self.myTableView]){
    if([indexPaths objectForKey:indexPath]){
        cell = [indexPaths objectForKey:indexPath];

    }else{
        if([letterArray objectAtIndex:indexPath.section]){
            static NSString *TableViewIdentifier = @"MyCells";
            cell = [[AddressBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text=[[[letterArray objectAtIndex:indexPath.section] objectAtIndex:0] valueForKey:@"name"];
            [[letterArray objectAtIndex:indexPath.section] removeObjectAtIndex:0];
            cell.checkbox.tag = indexPath.row;
            [cell.checkbox addTarget:self action:@selector(checkboxClicked:) forControlEvents:UIControlEventTouchUpInside];

        }

    }
    [indexPaths setObject:cell forKey:indexPath];

}
return cell;
}

这是我的 letterArray 的代码

-(void)setUpContacts{
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
eventController = [[UOEventDetailController alloc]init];
[tempArray addObject:[eventController comparingArrays]];



NSDictionary *alphabet = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:1],@"A",[NSNumber numberWithInt:2],@"B",[NSNumber numberWithInt:3],@"C",[NSNumber numberWithInt:4],@"D",[NSNumber numberWithInt:5],@"E",[NSNumber numberWithInt:6],@"F",[NSNumber numberWithInt:7],@"G",[NSNumber numberWithInt:8],@"H",[NSNumber numberWithInt:9],@"I",[NSNumber numberWithInt:10],@"J",[NSNumber numberWithInt:11],@"K",[NSNumber numberWithInt:12],@"L",[NSNumber numberWithInt:13],@"M",[NSNumber numberWithInt:14],@"N",[NSNumber numberWithInt:15],@"O",[NSNumber numberWithInt:16],@"P",[NSNumber numberWithInt:17],@"Q",[NSNumber numberWithInt:18],@"R",[NSNumber numberWithInt:19],@"S",[NSNumber numberWithInt:20],@"T",[NSNumber numberWithInt:21],@"U",[NSNumber numberWithInt:22],@"V",[NSNumber numberWithInt:23],@"W",[NSNumber numberWithInt:24],@"X",[NSNumber numberWithInt:25],@"Y",[NSNumber numberWithInt:26],@"Z", nil];


for(int i = 1; i<=27; i++){
    [tempArray addObject:[NSNull null]];
}
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
    firstLetter = [info objectForKey:@"lastName"];
    int index = 27;
    if([firstLetter length] > 0){
        firstLetter =[NSString stringWithFormat:@"%C",[firstLetter characterAtIndex:0]];

        firstLetter= [firstLetter capitalizedString];

        if([alphabet objectForKey:firstLetter]){

            NSNumber *t = [alphabet valueForKey:firstLetter];
            index = [t intValue];
        }
    }
    if([tempArray objectAtIndex:index] == [NSNull null]){
        [tempArray insertObject:[NSMutableArray array] atIndex:index];
    }
    [[tempArray objectAtIndex:index] addObject:info];
}
[alphabet release];    
NSArray *alphabet2 = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
NSMutableArray *tempArray2 = [[NSMutableArray alloc]init];
NSMutableArray *titleTemp = [[NSMutableArray alloc]init];
int c = 0;
for(int i = 0; i<=27; i++){
    if([tempArray objectAtIndex:i] != [NSNull null]){
        if(i == 0){
            [titleTemp insertObject:@"Suggested" atIndex:c];
        }else if(i == 27){
            [titleTemp insertObject:@"Others" atIndex:c];
        }else{
            int loc = i -1;

            [titleTemp insertObject:[alphabet2 objectAtIndex:loc] atIndex:c];
        }
        [tempArray2 insertObject:[tempArray objectAtIndex:i] atIndex:c];
        c++;
    }
}
[alphabet2 release];
[tempArray release];
letterArray = tempArray2;
titlePointer = titleTemp;
}
4

2 回答 2

0

在您的if([letterArray objectAtIndex:indexPath.section])声明结束时,您可以检查 lettersArray 以查看联系人是否应标记为选中,然后如果应该是,您可以调用[self checkboxClicked:cell.checkbox];以模拟单击复选框,以便更改其状态...

这是我能做的最好的事情,但我希望这会有所帮助。

于 2012-05-02T20:34:16.980 回答
0

1)您似乎没有重复使用单元格。看起来您为每一行分配一个新单元格并保留它。您应该查看表格视图的示例代码。(和 dequeueReusableCell... 的东西)

2)我看不到您实际设置复选框值的任何地方。您应该在 cellForRowAtIndexPath 中设置它,并且每次调用它时都应该设置它,因为它可能是具有旧值的重用单元格。

于 2012-05-02T21:08:11.637 回答