2017-05-13 10 views
0

이미지의 앞으로 버튼을 클릭하면 테이블 뷰 셀에 이미지를 표시해야하지만 스크롤 한 후에는 뷰 뷰 이미지가 변경됩니다. 셀의 각 버튼에 태그 값을 지정하고 배열의 모든 셀 참조를 저장했습니다 그리고 .. 순방향테이블 뷰 셀에 이미지 표시

을 버튼 i가 배열의 참조를 식별하고 그 특정 참조 영상을 변경하고 클릭에

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

    // static NSString *cellidentifier = @"cell"; 
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (cell == nil) { 
     cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    // cell.smallimage.image=[UIImage imageNamed:[_smallimagesarr objectAtIndex:indexPath.row]]; 
    //if ([str_scroll isEqualToString: @"scrolling"]) { 

    // }else{ 
    cell.bigimage.image=[UIImage imageNamed:[_bigimagesarr objectAtIndex:indexPath.row]]; 
    //} 

    cell.forwardbutton.tag =indexPath.row; 
    NSLog(@" %p",cell); 
    [cell.forwardbutton addTarget:self action:@selector(buttonInfo:) forControlEvents:UIControlEventTouchUpInside]; 
CellModel *cellmodel=[[CellModel alloc]init]; 
    cellmodel.tagValue=cell.forwardbutton.tag; 
    cellmodel.cellobj=cell; 
    [arrofcells addObject:cellmodel]; 
    NSLog(@"%p",cell); 
    return cell; 

} 


-(void)buttonInfo:(UIButton *)sender{ 
    UIButton *btn = (UIButton *)sender; 
    NSInteger tag = btn.tag; 

    CellModel *cmodel=[[CellModel alloc]init]; 

    cmodel=[arrofcells objectAtIndex:tag]; 
    democell=(LotsandLandTableCell *)cmodel.cellobj; 


    previoustag=cmodel.tagValue; 
if(cmodel.tagValue==previoustag) 
    { 

     if(j<_bigimagesarr1.count){ 
     democell.bigimage.image=[UIImage imageNamed:[_bigimagesarr1 objectAtIndex:j]]; 
     NSLog(@"j value is is %d",j); 
      j++; 

     }else{ 
      j=0; 
     [democell.forwardbutton setEnabled:NO]; 

     } 

    } 
} 

cellModel 셀 참조 및 속성 태그

포함 당신이있는 tableView, 그 셀을 재사용의 더 나은 이전 셀 데이터를 지울 때 너무 이전 이미지를 사용할 수 있기 때문에있는 tableView의 이미지가 변화하고 스크롤하면
+1

태그를 사용하지 마십시오. 왜 단순히 이미지 배열을 셀에 공급하고 셀 클래스 내의 버튼을 처리하지 않는 것이 좋을까요? 또한 셀을 다시 사용할 때마다 단추 처리기를 추가합니다. – Paulw11

+0

@ Paulw11 님이 더 이상 할 수 없었습니다. – tnishanth

+1

테이블보기 컨트롤러에서 버튼 탭을 처리하려고합니다. 즉, 태그를 사용하고있는 버튼 탭이 발생한 셀을 식별해야합니다. 셀은 재사용되고 재정렬 될 수 있기 때문에 태그가 깨지기 쉽습니다. 단순히 이미지 배열을 사용자 정의 셀 클래스의 속성으로 설정하면 각 셀에서 자체 단추 탭을 처리하고 해당 이미지 배열을 통해 이동할 수 있습니다. – Paulw11

답변

0

@interface CellModel : NSObject 
@property (nonatomic, assign) NSInteger tagValue; 
@property(nonatomic,strong) id cellobj; 
@end 
는 LotsandLandTableCell 클래스

-(void)prepareForReuse { 
    _bigimage.image = nil; 
} 

이 추가.

+0

정지 이미지가 변경됩니다. – tnishanth

0

행과 이미지를 표시하는 방식에 문제가 있습니다.

  1. 는 (귀하의 경우 CellModel으로도 작동 할 수 있습니다) 행의 데이터를 채우기위한 항목의 모델을 만들기 을 모델의 자산이 될 수 다음과 같습니다 :

      당신은 최상의 결과를 이하의 절차 필요

    • 가격
    • 설명
    • 지역
    • ,536을 항목 ID 등

imageNames 배열 (배열)

  • imageNames는 것입니다 당신이 기대에 표시 할 특정 행을 클릭 할 모든 이미지의 이름이 포함되어 있습니다.

    이 항목 객체를 LotsandLandTableCell 클래스에 할당하고 셀 자체에서 전달 버튼 콜백을 처리합니다. 앞으로 클릭하면 imagenNames 배열의 다른 이름을 선택하고 imageView로 설정합니다.

    위의 방법을 사용하면 문제가있는 화이트 아웃을 구현할 수 있습니다.

    모든 속성의 배열을 만들 수 있습니다 (하나의 항목 모델은 하나의 속성을 나타냅니다).

  • 2

    현재 코드는 사용하지 않습니다

    - (NSInteger)numberOfRowsInSection:(NSInteger)section 
    { 
        return items.count; 
    } 
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    
        // static NSString *cellidentifier = @"cell"; 
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
        if (cell == nil) { 
         cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
        } 
    
    cell.item = [items objectAtIndex:indexpath.row]; 
        return cell; 
    
    } 
    

    이 이제 사용자 정의 셀 수업 시간에 당신이 당신의 모든 셀에 데이터를 채울 나머지 로직을 구현할 수 있습니다 샘플 아래

    참조로 간주 될 수있다 관심사의 적절한 분리; 테이블 뷰 컨트롤러는 현재 셀에 표시되는 이미지를 신경 쓰지 않으므로 테이블 뷰 컨트롤러에서 단추 탭을 처리하는 것은 불필요하게 복잡합니다.

    소스 코드를 모두 표시하지 않았으므로 코드가 100 % 일치하지 않을 수 있습니다. - 나는 추측을합니다.또한 당신은 이미지의 두 가지 배열을 가지고있는 것처럼 보입니다. 나는 당신의 테스트 접근법이라고 생각합니다. 실제로는 셀당 하나의 이미지 배열을 가져야하며, 각 배열은 다른 데이터 구조에 포함되어야하며 배열은 테이블 뷰의 데이터 소스입니다.

    LotsandLandTableCell.h

    - (무효) setImages (NSArray를 *) 화상;

    LotsandLandTableCell.m

    @property (weak, nonatomic) NSArray *bigImages; 
    @property NSInteger imageIndex; 
    
    -(void) prepareForReuse { 
        self.bigImages = nil; 
        self.bigimage.image = nil; 
        self.imageIndex = 0; 
    } 
    
    -(void) setImages:(NSArray *)images { 
        if (images != null) { 
         self.bigImages = images; 
         self.imageIndex = 0; 
         [self updateBigImage]; 
        } 
    } 
    
    @IBAction forwardTapped:(UIButton *)sender { 
        if (self.images.count == 0) { 
         return; 
        } 
        self.imageIndex = (self.imageIndex + 1) % self.images.count; 
        [self updateBigImage]; 
    } 
    
    @IBAction backwardTapped:(UIButton *)sender { 
        if (self.images.count == 0) { 
         return; 
        } 
        self.imageIndex -= 1; 
        if (self.imageIndex < 0) { 
         self.imageIndex = self.bigImages.count; 
        } 
        [self updateBigImage]; 
    } 
    
    - (void) updateBigImage { 
        self.bigimage.image = [UIImage imageNamed:self.images[self.imageIndex]]; 
    } 
    

    보기 컨트롤러

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
        LotsandLandTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
        if (cell == nil) { 
         cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
        } 
    
        NSArray *yourImagesArray = yourDataArray[indexPath.row].bigImages; // Change this 
    
        [cell setImages:yourImageArray]; 
    
        return cell; 
    } 
    
    관련 문제