2013-02-17 2 views
0

내 tableview에서 그림을 변경하려고합니다. 내 TableView을 새로 고치기 위해 [myTableView reloadData];을 다른 방법으로 사용하고 있습니다. 그리고 나는 tableview의 다른 요소가 상쾌하지만 이미지가 그대로 유지되는 것을 볼 수 있습니다. 나는 무엇을 놓치고 있습니까?UITableView 내에서 그림을 새로 고칠 수 없습니다.

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UILabel *lblName; 
    UIImageView *checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)]; 
     backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3); 
     backgroundView.userInteractionEnabled = YES;   

     checkMark.image = [UIImage imageNamed:@"V.png"]; 
     checkMark.contentMode = UIViewContentModeCenter; 
     checkMark.backgroundColor = [UIColor clearColor]; 
     [backgroundView addSubview:checkMark]; 
     [checkMark release]; 

     [cell.contentView addSubview:backgroundView]; 
     [backgroundView release]; 
    } 
    if (something) 
    { 
     checkMark.image = [UIImage imageNamed:@"halfleft.png"]; 
     NSLog(@"something was done"); 
    } 
    return cell; 

} 
+1

전 '의 경우'} '마지막 (무언가) '를 바로 아래로 이동해야합니다. [= [[UITableViewCell alloc] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : CellIdentifier] autorelease];' – Groot

+0

왜 그런가요? 셀이 nil이 아니라면 하위 뷰를 계속 추가하고 싶지 않습니다. – Segev

+1

하지만 동일한 셀 rigth에 새로운 이미지가 나타나야합니까? 따라서 셀이 없으면 그냥 셀만 만들면됩니다. 다른 모든 것들은 바깥쪽에 있어야합니다 (셀 == nil). – Piyuesh

답변

1

reloadData는이 메서드를 올바르게 호출합니다.

문제가 간단합니다. 셀을 "다시 사용"한 경우 "checkMark"의 포인터가 좋지 않습니다. 이런 식으로 뭔가를 수행합니다.

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UIView *backgroundView; 
    UIImageView *checkMark; 
    if (cell == nil) { 

     checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)]; 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)]; 
     backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3); 
     backgroundView.userInteractionEnabled = YES; 
     backgroundView.tag = 555; 

     checkMark.image = [UIImage imageNamed:@"V.png"]; 
     checkMark.contentMode = UIViewContentModeCenter; 
     checkMark.backgroundColor = [UIColor clearColor]; 
     checkMark.tag = 666; 
     [backgroundView addSubview:checkMark]; 
     [checkMark release]; 

     [cell.contentView addSubview:backgroundView]; 
     [backgroundView release]; 
    } else { 
     backgroundView = (UIImageView *)[cell.contentView viewWithTag:555]; 
     checkMark = (UIImageView *)[backgroundView viewWithTag:666]; 
    } 
    if (something) 
    { 
     checkMark.image = [UIImage imageNamed:@"halfleft.png"]; 
     NSLog(@"something was done"); 
    } 
    return cell; 

} 

을, 당신은 더 간단한 일 (이하 파단 가능하다면 어떻게해야 또는 확인 표시 ;-)에 대한 포인터를 가지고있는 UITableViewCell 하위 클래스

+0

코드가 작동하지만 반복적 인 응답이 있습니다. 'checkMark.image'도 다른 셀에도 추가 된 것을 볼 수 있습니다. 또한 여기에 사용 된 태그가 무엇인지 설명해 주시겠습니까? – Segev

+0

코드가 어떻게 작동해야하는지 모르겠습니다. 그러나 셀 생성시 어떤 .image (if (cell == nil) {})를 사용하지 마십시오. 태그를 사용하면보기를 식별 할 수 있으므로 재사용 할 때 찾을 수 있습니다. 그래서 if (cell == nil)에서 "동적"내용을 제거하고 다음 코드에서 수행하십시오 : – Vinzius

+0

문제는 셀 외부에서 하위 뷰를 추가하면 모든 UITableView 스크롤이 느리고 응답이 늦어집니다. . 그게 일어날 것 같니? 어쩌면 내가 viewDidLoad (나는 또 다른 문제 ..)에서 myTable을 공개하지 않는다는 사실과 관련이있다. – Segev

관련 문제