2013-04-25 71 views
0

我在我的UITableViewCell中遇到了uiswitch問題,每當我在屬於特定部分的特定單元格中更改開關值時。所有其他部分單元格具有相同的inexPath.row更改。請幫忙 ?重新使用表格單元格中的UI開關

這裏是我的cellForRowAtIndexPath方法的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    //UISwitch *switchview = nil ; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 



    } 


    UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero]; 
    CGRect switchFrame = switchController.frame; 
    NSString *str = [[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    NSLog(@"string in cell%@", str); 


    //set its x and y value, this you will have to determine how to space it on the left side 
    switchFrame.origin.x = 50.0f; 
    switchFrame.origin.y = 10.0f; 
    switchController.frame = switchFrame; 


    [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
    [cell addSubview:switchController ]; 
    [addSubview:switchController]; 


    if ([[[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row ]isEqualToString:@"ON"]) 
{ 
     switchController.on=YES; 
} 
    else 
    { 
     switchController.on=NO; 
    } 


    return cell; 

} 

這裏是SwitchChangedEvent:

-(void)switchChanged:(UISwitch *)sender 
{ 
    UITableViewCell *cell = (UITableViewCell *)[sender superview]; 
    NSIndexPath *index=[mainTableView indexPathForCell:cell]; 

    if (sender.on) 
    { 

     [[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"ON"]; 
     NSString *word= [[self.mainArray objectAtIndex:index.section ] objectAtIndex:index.row]; 

    } 
    else 
    { 
     //call the first array by section 
     [[self.SwitchArray objectAtIndex:index.section] replaceObjectAtIndex:index.row withObject:@"OFF"]; 
     NSString *word= [[self.mainArray objectAtIndex:index.section ] objectAtIndex:index.row]; 


    } 

    [padFactoids setObject:[NSKeyedArchiver archivedDataWithRootObject:SwitchArray] forKey:@"savedArray"]; 
    [padFactoids synchronize]; 

} 
+0

您有問題嗎? – matt 2013-04-25 19:44:15

+0

'switchChanged:'中的代碼看起來像什麼? – jszumski 2013-04-25 19:44:45

+0

@jszumski這是它: – 2013-04-25 19:47:49

回答

0

如果你看看,我想你會發現你正在建立多個交換機和各細胞。

隨着單元重新使用,您添加一個新開關但不刪除舊單元。我認爲這至少是你問題原因的一部分。


UPDATE

我想我會以不同方式處理這一點比其他人。我認爲@AlanoudAldrees將更容易刪除舊開關。

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 
} 
else 
{ 
    for (UIView *view in cell.subviews) 
     if ([view isKindOfClass:[UISwitch class]]) 
      [view removeFromSuperview]; 
} 
+0

你能解釋更多嗎?因爲我也認爲這是我的問題謝謝 – 2013-04-25 20:09:22

+0

@AlanoudAldrees我更新了我的答案。 – 2013-04-26 15:08:56

3

你有兩個主要問題:

  1. 這段代碼是錯誤的:

    [cell addSubview:switchController ]; 
    

    絕不添加一個子視圖到細胞;將其僅添加到單元的contentView

  2. 您每次都通過cellForRowAtIndexPath:添加開關。但是這些細胞正在被重複使用。所以有些單元已經這個開關。因此,您正在向某些單元添加許多開關。

+0

非常感謝您的快速回復,這意味着很多..但是,請您給我詳細說明如何解決第二點問題?我將不勝感激這麼多,你將會是一種生活的節約 – 2013-04-25 20:04:59

+0

你已經在測試這是否是「virgin」單元格,並帶有if(cell == nil)'。這種情況也是您需要添加交換機的地方。這樣,如果單元格是* not * nil,您肯定知道它有一個開關。 – matt 2013-04-25 20:13:29

+0

它可能會幫助您閱讀我關於此主題的書籍章節:http://www.apeth.com/iOSBook/ch21.html#_table_view_cells – matt 2013-04-25 20:16:20

0

正如其他人已經指出的那樣,您要爲每個單元添加多個開關。 FTFY:

UISwitch *switchController = nil; 
if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier]; 
    switchController = [[UISwitch alloc] initWithFrame:CGRectZero]; 
    switchController.tag = 'swch'; 
    CGRect switchFrame = switchController.frame; 
    //set its x and y value, this you will have to determine how to space it on the left side 
    switchFrame.origin.x = 50.0f; 
    switchFrame.origin.y = 10.0f; 
    switchController.frame = switchFrame; 
    [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
    [cell.contentView addSubview:switchController]; 
} 
else 
{ 
    switchController = (UISwitch*)[cell.contentView viewWithTag: 'swch']; 
    NSAssert(switchController != nil, @"how?"); 
} 

NSString *str = [[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
NSLog(@"string in cell%@", str); 

switchController.on = [str isEqualToString:@"ON"]; 

return cell; 
+0

我強烈勸阻'switchController.tag ='swch''。見:http://stackoverflow.com/questions/7755202/multi-character-constant-warnings – 2013-04-26 14:51:19

+0

@JefferyThomas很高興知道。雖然,請幫助我理解爲什麼在這個特定情況下這是一個壞主意?我在這裏看不到任何可移植性問題。 – Mar0ux 2013-04-26 14:55:08

+0

int x ='xxxx'在標準C中是未定義的行爲(GCC和LLVM具有實現定義的行爲)。我對它的真正問題是,它隱藏了標籤的含義。標記是一個數字,不是4個字符的序列。 – 2013-04-26 15:23:00

相關問題