2013-05-31 5 views
0

내 셀의 내용을 동적으로 수정하고 싶습니다. 데이터를 다시로드 한 후에도 실제로 작동하지 않습니다. 예상대로 테이블로드가 1 1 1 1 1 1 1이 될 때 세그먼트 1을 누르면 예상대로 4 4 4 4 4 4 4가 나오지만 2를 누르면 -1이됩니다. 1 -1 -1 -1 -1 다시 그리고 3을 누르면 4 4 4 4 4 4 4가 다시 나타납니다. 또한, 내 세포의 내용은 내가 버튼을 누를 때의 순서에 달려 있습니다. 내 코드는 테이블이 하나의 행과 많은 부분이 아래 :동적으로 셀 텍스트 수정

당신은 if (cell == nil) 블록의 외부 셀을 구성하는 코드를 이동해야
- (void)viewDidLoad 
{ 
currentarray=[NSArray arrayWithObjects:@"-1",@"-2",@"-3", @"-4", nil]; 
} 
- (IBAction)SegControl: (id) sender{ 

for(int i=0; i<4; i++){ 
    if(theSegControl.selectedSegmentIndex==i){ 
     buttonpressed[i]=[NSNumber numberWithInt:1]; 
    } 
    else { 
     buttonpressed[i]=[NSNumber numberWithInt:0]; 
    } 
} 

if([buttonpressed[0] intValue]==1){ 

    currentarray=sorteddectotalteamPoints; 

} 

else if([buttonpressed[1] intValue]==1){ 

    currentarray=[NSArray arrayWithObjects:@"4",@"6",@"7", @"8", nil]; NSLog(@"%@",@"two pressed"); 
} 

else if([buttonpressed[2] intValue]==1){ 

    currentarray=[NSArray arrayWithObjects:@"9",@"10",@"11", @"12", nil]; NSLog(@"%@",@"three pressed"); 
} 

else { 

    currentarray=[NSArray arrayWithObjects:@"13",@"14",@"15", @"16", nil]; NSLog(@"%@",@"four pressed"); 
} 

//to update the cells  
for(int i=0; i<[teamnameswithoutRepeat count];i++){ 
    [byTeam reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade]; 
} 
    NSLog(@"%@",currentarray); 


    } 

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

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, tableView.rowHeight)] ; 

label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14]; 
label.layer.borderColor = [UIColor whiteColor].CGColor; 
label.layer.borderWidth = 1.0; 


label.textAlignment = NSTextAlignmentCenter; 
label.textColor = [UIColor blackColor]; 
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.section]; 

MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil) { 

    cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:MyIdentifier] ; 
    if(tableView==byTeam) 
    { 

     //[tableView reloadData]; 
     label.text=[currentarray objectAtIndex:0]; 
     [cell.contentView addSubview:label]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     // UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 
     //cell.textLabel.text = @"updated text"; 




     } 

     } 
     } 

답변

0

당신이 세포를 다시로드 할 때 실행됩니다 그들이 dequeueReusableCellWithIdentifier:에 의해 재활용 얻을 있도록 : 뭔가를 누락하지 않는 한 또한

if (cell == nil) { 

    cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault 
          reuseIdentifier:MyIdentifier] ; 
} 

if(tableView==byTeam) { 
    label.text=[currentarray objectAtIndex:0]; 
    //... 
} 

, 당신의 세그먼트 제어 핸들러는 buttonPressed 변수를 제거하여 상당히 단순화 할 수있다. 예를 들면 다음과 같습니다.

- (IBAction)SegControl:(id)sender 
{ 
    switch(sender.selectedSegmentIndex) { 
     case 0: 
      currentArray = sorteddectotalteamPoints; 
      break; 
     case 1: 
      currentArray = [NSArray arrayWithObjects:@"4",@"6",@"7", @"8", nil]; 
      break; 
     case 2: 
      currentArray = [NSArray arrayWithObjects:@"9",@"10",@"11", @"12", nil];; 
      break; 
     case 3: 
      currentArray = [NSArray arrayWithObjects:@"13",@"14",@"15", @"16", nil]; 
      break; 
    } 

    //to update the cells  
    for(int i=0; i<[teamnameswithoutRepeat count];i++) { 
     [byTeam reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
}  
+0

감사합니다. 엄지 위로! – snowleopard

+0

나는 당신이 이미 알고있는 것을 알고 있지만 당신이 더 설명해 줄 수 있었기 때문에 나는 그것을 cell = nil statement 밖으로 옮겨야 만했다. 감사합니다 – snowleopard

+0

물론입니다. 셀을 다시 불러올 때,'dequeueReusableCellWithIdentifier :'는 당신이 다시로드하는 셀 중 하나를 재활용하고 그것을 반환합니다. 따라서'cell' 변수는'if (cell == nil) '문에 도달하기 전에 이미 설정되어 있으므로 프로그램은 결코 그 블록에 들어 가지 않습니다. 처음에는 재활용 할 셀이없고 'dequeueReusableCellWithIdentifier :'는'nil'을 반환하므로 프로그램이 블록을 입력하게되므로 첫 번째 패스에서 올바르게 작동합니다. –