2011-08-12 6 views
1

UITableViewCellUILabelUISwitchです。デフォルトでは、すべてUISwitchがオフに設定されています。 //以下はUITableViewセルが再生中です

- (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]; 
    } 

} 

私はスイッチをオンにして、スイッチの値が再びデフォルトに設定されているテーブルをスクロールしますたら、下記

ieOff

は、私が使用しているコードです私の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; 
} 

私はここで何か間違っていますか?

ありがとうございます。

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

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

はあなたには、いくつかの留保対象に、あなたのスイッチの状態をバインドしています(例:項目モデルオブジェクト、単一のセルが反映:あなたはそれが必要だたびにセルを再生成し、本当に厄介なコードを使用している

答えて

1

項目)? Iすべて "initWithFrame" を行う{...}私の場合(セル== nilの)内、

基本的には:

+0

私はそれを使用していない場合、ラベルはあなたが、あなたの答えを持ってそこ – spaleja

+0

を上書きしている:)保持対象とする場合、セルはちょうどそれを再利用する必要があるにあなたのスイッチの状態をバインドするようにしてください。 –

+0

どうすればいいですか? – spaleja

0

は、これは私が私のセルの描画コードを書く方法です。それ以外のものは、ラベルテキストなどの値を設定するだけです。 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; 
} 
関連する問題