2013-08-20 1 views
0

각 Uuitableview 행에 UIButton을 추가했지만, reloadData를 지정하면 uibutton은 0x0000입니다. 제안 해 줄 수 있니? 감사합니다. 은이 코드를 사용 : reloadData, 가격 라벨의 데이터가 잘 변경하지만 marketbutton 이미지를 변경할 수 없습니다UIButton은 iOS에서 TableView를 다시로드 할 때 0x0000입니다.

나는 위의 코드를 사용
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    /*NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row]; 
    // NSString *CellIdentifier = @"cell"; 
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    {*/ 
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 


     UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)]; 
     pricelabel.backgroundColor = [UIColor clearColor]; 
     pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
     pricelabel.font = [UIFont boldSystemFontOfSize:16]; 
     pricelabel.textColor = [UIColor darkGrayColor]; 
     pricelabel.tag = 3000; 
     //pricelabel.hidden = YES; 
     pricelabel.textAlignment = NSTextAlignmentRight; 
     [cell.contentView addSubview: pricelabel]; 
     [pricelabel release]; 


     UIButton * market = [[UIButton alloc] init];; 
     [market setFrame:CGRectMake(200, 6, 30, 30)]; 
     market.tag = 4000; 

     [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; 

     // [market setTag:indexPath.row]; 

     [cell.contentView addSubview:market]; 
    } 


    if([priceNewArray count]> 0) 
    { 
     UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000]; 
     pricelbl.text =[NSString stringWithFormat:@"$%@",[priceNewArray objectAtIndex:indexPath.row]]; 
     if ([sellingArray count]>0) { 
      if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]){ 
       pricelbl.hidden = NO; 
      } 
      else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]){ 
       pricelbl.hidden = YES; 

     } 

    } 


    } 
    UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000]; 
    [marketButton setTag:indexPath.row]; 
    if([sellingArray count]>0) 
    { 
     NSLog(@"sellingArray %@",sellingArray); 
     if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing 
     { 

      [marketButton setSelected:NO]; 
      [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; 
      marketButton.enabled = YES; 

     } 
     else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace 
     { 

      [marketButton setSelected:YES]; 
      [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; 
      marketButton.enabled = YES; 

     } 
    } 

    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0); 

    return cell; 
} 

. 전화 [_tableview reloadData];, marketButton이 0x0000이면 디버깅합니다.

+2

각 행에 다른 셀 식별자를 사용하는 이유는 무엇입니까? 세포 재활용의 혜택을 잃어 가고 있습니다. 네가 말하는 가치관을 어디서 어떻게 확인하고 있니? – Abizern

+0

@Abizern : 예제 코드를 사용할 수 있습니까? 나는 초보자입니다. 고마워 –

+0

샘플 코드가 아니라 힌트입니다. 스토리 보드에서 사용자 정의 셀을 디자인하고 코드에서 수행하는 것보다 UI 요소를 추가하는 것이 훨씬 쉽습니다. – Abizern

답변

0

귀하의 비교가 올바르지 않다고 생각합니다. 정수와 문자열을 비교하려고합니다.

만약 내가 asumed 올바른 다음이 같은 시도. 셀에 대해 동일한 디자인을 사용하는 경우 셀

int sellingPrice=0; 

if([[sellingArray objectAtIndex:indexPath.row] isEqualToString: [NSString stringWithFormat:@"%i",sellingPrice]]) // nothing 

sellingPrice=2; 

else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:[NSString stringWithFormat:@"%i",sellingPrice]]) // marketplace 
+0

아니요, 비교가 올바르지 않다면 가격표가 아직 다시로드 되었습니까? –

0

사용 하나 CellIdentifier, 그것은 당신이 셀에 대한 태그를 변경하지

NSString *CellIdentifier = @"Cell" 

이 코드 [marketButton setTag:indexPath.row];을 제거하십시오있는 tableview 세포를 재사용하는 데 사용, 당신은 설정하지 태그 식별자가 인 경우 UIButton의 경우이 두 코드를 변경하면 코드가 작동합니다.

+0

[marketButton setTag : indexPath.row]를 제거하면; marketbutton을 누르면 앱이 다운됩니다. –

+0

marketPressedAction 메소드가 클래스에 있습니까? 버튼 이미지가 변경 되었습니까? – NANNAV

+0

예, 내 수업의 MarketPressedAction 메소드는 무엇입니까? 버튼 이미지가 변경되지 않았습니다. –

관련 문제