UITableView에 배치 된 게시물이있는 앱이 있습니다. 각 게시물에는 좋아하는 버튼이있어서 사용자가 클릭 할 때 이미지를 변경해야합니다.업데이트 할 때 충돌이 발생합니다. iOS의 UITableViewCell 내의 UIButton 이미지
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"myNib" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *post = [posts objectAtIndex:indexPath.row];
[cell.likeButton addTarget:self
action:@selector(clickedOnLike:)
forControlEvents:UIControlEventTouchUpInside];
cell.likeButton.tag = indexPath.row;
클릭 핸들러 : 여기에 코드입니다이 시점에서
-(IBAction)clickedOnLike:(id)sender
{
int tag = buttonSender.tag;
NSDictionary *post = [posts objectAtIndex:tag];
if(![self likedAlready:post]){
//set liked on this view...
//update view
NSLog(@"button: %@",buttonSender);
[sender setImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
//send like to server...
}
}
, 모든 것이 괜찮아 것입니다. 문제는 단추를 클릭하고보기를 업데이트하고 다른 셀로 스크롤 한 후 결코 클릭하지 않은 다른 단추보기도 업데이트된다는 것입니다. 예를 들어, indexPath 1의 버튼을 클릭하면 5와 9의 버튼이 자동으로 이미지를 변경합니다. 이것은 액션 발신자를 직접 호출하고 업데이트하기 때문에 나에게 미스터리입니다. 도와 주셔서 감사합니다.
세포가 재사용되기 때문에 "addTarget"호출은 여전히 남아 있습니다. (아마 그들은 쌓여있을 것입니다). 어쩌면 cellForRowAtIndexPath 시작 부분에서 제어 이벤트에 대한 작업을 제거해야합니다. – EDUsta