2012-01-18 81 views
0

我有一个tableview,其中有很多条目分为多个部分,里面有多行。我在每行中都有一个按钮模型化复选框。按下时可以启用/禁用复选框(按钮)。我正在加载一个图像的按钮(复选框)根据状态,无论是否检查。一切都很顺利,直到我试图向下拖动我的UITableview,然后往上走,才发现我的一些选中的复选框会自动取消选中。我有点认为,这是由于使用当uitableview向上和向下滚动时,复选框状态发生变化

dequeuereusablecellwithidentifier 

我想避免我的tableview这种奇怪的行为。在这种情况下需要帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    UIButton *checkBox = [[UIButton alloc] init]; 
    checkBox.tag = contact.contactID; 
    [cell.contentView addSubview:checkBox]; 
    [checkBox setFrame:CGRectMake(6,14,20,20)]; 
    [checkBox release]; 
} 
UIButton *checkBox = (UIButton *)[cell.contentView viewWithTag:contact.contactID]; 

if(isActivDeactivButton) 
{ 
    [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
    } 
else{ 
    [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal]; 
} 
return cell; 
} 

回答

0

我更换

checkBox.tag = contact.contactID; 

checkBox.tag = 111; 

这解决了问题,因为我的ContactID值滚动过程中发生变化。

2

确保您的-tableView:cellForRowAtIndexPath:逻辑设置如下:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     // this part is called when the cell is created anew 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     // add new components here (this is where you'd add your checkbox control, etc) 
    } 

    // change the state of components here (this is where you'd make sure your checked property is set correctly 

    return cell; 
} 

使用dequeueReusableCellWithIdentifier:是内存管理的伟大实践,所以你要保持这个是肯定的。

+0

我已将我的代码添加到问题中。 – 2012-01-18 18:46:51

+0

这就是你的全部代码吗?我没有看到'contact','isActivDeactivButton'从哪里来。 – Tim 2012-01-18 19:00:42

+0

在您的代码中添加一个NSLog(@“在第%d行的载入单元格”,indexPath.row);然后在滚动时观察输出 - 这将帮助您更好地了解它何时调用方法并会帮助你找出为什么它没有正确设置。 – Tim 2012-01-18 19:01:28

2
// Add following methods def in viewController .h 

    -(void)checkButtonPressed:(id)sender; 
    -(void)addSelectedCheckBoxTag:(int)value; 
    -(void)deleteSelectedCheckBoxTag:(int)value; 
    -(BOOL)isSelectedCheckBox:(int)value; 

// add target to the check button in cellForRowAtIndexPath - 

    [checkBox addTarget:self action:@selector(checkButtonPressed:) forControlEvents:UIControlStateNormal]; 

// Ad following methods in viewController .m where tabelView 

    -(void)checkButtonPressed:(id)sender 
    { 
    UIButton *checkBox=(UIButton*)sender; 
    if(checkBox.selected) 
    { 
    checkBox.selected=false; 
    [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
    [self deleteSelectedCheckBoxTag:checkBox.tag]; 
    NSLog(@"unselected .."); 
    } 
    else 
    { 
    checkBox.selected=true; 
    [self addSelectedCheckBoxTag:checkBox.tag]; 
    [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];  
    NSLog(@"selected.."); 
    }  

} 

    // Add element in to array if already present forget 

    -(void)addSelectedCheckBoxTag:(int)value 
    { 
     int flag=0; 

    for(int i=0;i<[arrayForTag count];i++) 
    {   
     if([[arrayForTag objectAtIndex:i] intValue]==value) 
     flag=1; 
    } 
    if(flag==0) 
    [arrayForTag addObject:[NSString stringWithFormat:@"%d",value]]; 
    } 

    // delete element add in array if present 

    -(void)deleteSelectedCheckBoxTag:(int)value 
    { 

    for(int i=0;i<[arrayForTag count];i++) 
    {   
     if([[arrayForTag objectAtIndex:i] intValue]==value) 
     [arrayForTag removeObjectAtIndex:i]; 
    } 
    } 

    // For take is selected or not from array - 

-(BOOL)isSelectedCheckBox:(int)value 
    { 
    for(int i=0;i<[arrayForTag count];i++) 
    {   
     if([[arrayForTag objectAtIndex:i] intValue]==value) 
     return true; 
     } 
    return false; 
    } 

    // In cellForRowAtIndexPath replace following code 

    if(isActivDeactivButton) 
     { 
     [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
     } 
else{ 
    [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal]; 
    } 

    // with following code - 

    if(![self isSelectedCheckBox:checkBox.tag]) 
    { 
     [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
    } 
    else{ 
     [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal]; 
     } 
+0

这是一个非常详细和翔实的答案。多谢兄弟。 – 2012-01-19 14:32:04

+0

详细答案。也为我工作。谢谢 – 2012-10-01 20:34:23

相关问题