2011-08-12 35 views
1

我有UITableViewCellUILabelUISwitch。默認情況下,所有UISwitch設置爲關閉。UITableView單元正在再生

一旦我將打開開關,然後通過表滾動開關值被設置爲默認值再次,ieOff

下面是我所使用的代碼:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell != nil) cell = nil; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    if (indexPath.row == 0) { 
     UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)]; 
     lbl1.text = @"Some Text"; 
     [cell addSubview:lbl1]; 

     switch = [[UISwitch alloc] initWithFrame:CGRectMake(190, 10, 200, 30)]; 
     [switch setOn:NO]; 
     switch.tag = 1; 
     [switch addTarget:self action:@selector(switchTapped:) forControlEvents:UIControlEventChanged]; 
     [cell addSubview:switch]; 
    } 

} 

//下面是我的switchTapped方法:

- (void) switchTapped: (id)sender { 
    UISwitch *tapSwitch = (UISwitch *)sender; 

    switch (tapSwitch.tag) { 
     case 1: 
      if (tapSwitch.on) { 
       // do something 
      } 
      else { 
       // do something 
      } 
      break; 
     case 2: 
      if (tapSwitch.on) { 
       // do something 
      } 
      else { 
       // do something 
      } 
      break; 
} 

我在這裏做錯了什麼嗎?

謝謝。

回答

1

你使用真正討厭的代碼需要它的每一次這再生細胞:其中,單電池反映了

if(cell != nil) 
{ 
    cell = nil; 
} 

if (cell == nil) 
{ 
... 
} 

你的開關的狀態綁定到一些保留的對象(例如項目模型對象,項目)?

+0

如果我不使用它,標籤有覆蓋 – spaleja

+0

你有你的答案呢:)儘量把開關的狀態綁定到保留的對象,並且需要一個單元格時只是重複使用它。 –

+0

你能告訴我怎麼做到這一點嗎? – spaleja

0

這是我寫我畫的細胞代碼:

基本上,我如果(細胞==零)內{...}我做的所有「initWithFrame」。除此之外,我只需設置標籤文本等值。不要在if(cell == nil){...}代碼塊外執行initWithFrame。

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath 
{ 
    static NSString *reusableCell = @"reusableCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableCell]; 

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

     thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 106, 81)]; 

     feedTitle = [[UILabel alloc] initWithFrame:CGRectMake(116, 3, [IOSDevice screenWidth] - 140, 25)]; 
     postDate = [[UILabel alloc] initWithFrame:CGRectMake(116, 10, [IOSDevice screenWidth] - 140, 50)]; 
     description = [[UILabel alloc] initWithFrame:CGRectMake(116, 45, [IOSDevice screenWidth] - 140, 50)]; 

     // setting tag reminders so we can identify the element to replace 
     [thumbnail setTag:1]; 
     [feedTitle setTag:2]; 
     [postDate setTag:3]; 
     [description setTag:4]; 

     [[cell contentView] addSubview:thumbnail]; 
     [[cell contentView] addSubview:feedTitle]; 
     [[cell contentView] addSubview:description]; 
     [[cell contentView] addSubview:postDate]; 

     [thumbnail release]; 
     [feedTitle release]; 
     [description release]; 
     [postDate release]; 
    } 

    thumbnail = (UIImageView *)[[cell contentView] viewWithTag:1]; 
    feedTitle = (UILabel *)[[cell contentView] viewWithTag:2]; 
    postDate = (UILabel *)[[cell contentView] viewWithTag:3]; 
    description = (UILabel *)[[cell contentView] viewWithTag:4]; 

    [feedTitle setBackgroundColor:[UIColor clearColor]]; 
    [feedTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]]; 
    [feedTitle setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]]; 

    [description setBackgroundColor:[UIColor clearColor]]; 
    [description setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
    [description setTextColor:[UIColor colorWithRed:0.328 green:0.328 blue:0.328 alpha:1.0]]; 
    [description setNumberOfLines:2]; 
    [description setLineBreakMode:UILineBreakModeWordWrap]; 

    [postDate setBackgroundColor:[UIColor clearColor]]; 
    [postDate setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
    [postDate setTextColor:[UIColor colorWithRed:0.707 green:0.180 blue:0.141 alpha:1.0]]; 

    [thumbnail setImage:[[items objectAtIndex:[indexPath row]] objectForKey:@"thumb"]]; 

    [feedTitle setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"title"]]; 
    [description setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"summary"]]; 

    // Format date 
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateStyle:NSDateFormatterLongStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 

    [postDate setText:[dateFormatter stringFromDate:[[items objectAtIndex:[indexPath row]] objectForKey:@"date"]]]; 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

    if([feedList contentOffset].y < -50) 
    { 
     shouldUpdate = TRUE; 

     [activityIndicator stopAnimating]; 

     [feedList setContentOffset:CGPointMake(0, -30) animated:NO]; 
     [self loadData]; 

     loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -25, [IOSDevice screenWidth], 20)]; 
     [loadingLabel setText:@"Loading New Data"]; 
     [loadingLabel setTextAlignment:UITextAlignmentCenter]; 
     [loadingLabel setBackgroundColor:[UIColor clearColor]]; 
     [loadingLabel setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]]; 
     [loadingLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]]; 

     reloadingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(70, -25, 20, 20)]; 
     [reloadingSpinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; 
     [reloadingSpinner startAnimating]; 
     [reloadingSpinner setHidesWhenStopped:YES]; 

     [feedList addSubview:reloadingSpinner]; 
     [feedList addSubview:loadingLabel]; 
    } 

    return cell; 
}