我正在创建一个显示用户联系人列表和建议联系人的应用。我希望像这样预先选择建议的联系人部分的复选框。有人帮帮我吗?
截至目前,我不知道如何实现这一点。当我尝试时,所有复选框都被预选。
这是我的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;
}
答案 0 :(得分:0)
在if([letterArray objectAtIndex:indexPath.section])
语句结束时,您可以检查lettersArray,看看联系人是否应该标记为已选中,然后是否可以调用[self checkboxClicked:cell.checkbox];
来模拟复选框的点击它会改变它的状态......
这是我能做的最好的事情,如果不深入研究它,但我希望有所帮助。
答案 1 :(得分:0)
1)您似乎没有重复使用单元格。看起来你为每一行分配一个新的单元格并保留它。您应该查看表视图的示例代码。 (以及dequeueReusableCell ......东西)
2)我没有看到你实际设置复选框值的任何地方。你应该在cellForRowAtIndexPath中设置它,你应该在每次调用它时设置它,因为它可能是一个具有旧的陈旧值的重用单元格。