내가 만든 셀이있는 테이블보기에 대한 코드가 있습니다. 각 셀에는 3 개의 버튼이 있습니다. 각 버튼에는 이미지가 배경 이미지로 있어야합니다.3 버튼이있는 테이블보기
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CellView" owner:self options:nil];
cell = nibLoadedCell;
}
for (int i = 1; i < 4; i++) {
//----- select the correct button from the cell
button = (UIButton *)[cell viewWithTag:(i)];
//----- cleaning the button, drawing it's corners, borders and giving it an action
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
//------ Setting up the btnImg in order to place on the buttons (if is for checking i'm not taking pictures out of the array bounds)
UIImage *btnImg = [[UIImage alloc] init];
if (((indexPath.row * 3) + i - 1) < photosArray.count) {
btnImg = [photosArray objectAtIndex:((indexPath.row * 3) + i - 1)];
//NSLog(@"I am at indexpath.row: %d",indexPath.row);
//NSLog(@"I show image number %d in the array",[photosArray indexOfObject:btnImg]);
NSLog(@"Expression is: %d",((indexPath.row * 3) + i - 1));
}
//------ Add btnImg to the button
[button setBackgroundImage:btnImg forState:UIControlStateNormal];
//------ Giving special tags for each button in order to recognize it later
button.tag = imageButtonTag;
imageButtonTag--;
}
return cell;
}
당신이 볼 수 있듯이, 내 코드에서 루프에 대한 내 "photosArray"에서 이미지를 복용하고 올바른 버튼에 배치의 담당 :
이것은 CellForRowArIndex 내 코드입니다 세포.
문제 : 첫 번째 ~ 23 개의 버튼이 올바른 이미지를 얻습니다. 그 다음, 이미지 24를 시작하면서 (조금 아래로 스크롤해야합니다.) 이미지가 반복됩니다. 이미지 24는 이미지 0과 같습니다. 이미지 25는 이미지 1과 동일합니다 ...
이미 많은 것들을 확인했습니다. 모든 게 잘됐다! 나는 심지어 photosArray indexOfObject를 체크했고 숫자가 정확했다.
이 방법으로 어레이를 만들 수 있습니다. 나도 그렇게 생각 했어. 근데 너 괜찮아. 내가 루프 내를 삭제하고 대신 다음 코드를 입력 :
//----- select the correct button from the cell
button = (UIButton *)[cell viewWithTag:(1)];
//----- cleaning the button, drawing it's corners, borders and giving it an action
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
//------ Setting up the btnImg in order to place on the buttons
UIImage *btnImg = [photosArray objectAtIndex:indexPath.row];
//------ Add btnImg to the button
[button setBackgroundImage:btnImg forState:UIControlStateNormal];
이 각 셀에 하나 개의 버튼으로 나에게있는 tableView를 제공하고 버튼은 오른쪽 이미지를 얻을 ... 내가 노력
마지막 일을,이다 바로 btnImg를 만든 후 내 이전 코드 코드 :
if ([btnImg isEqual:[photosArray objectAtIndex:0]]) {
NSLog(@"Match found at: %d",[photosArray indexOfObject:btnImg]);
}
그러나 (내가 그것을 실행할 때 내가 이미지가 동일 참조하지만) 아무것도 일치하지 않습니다.
이 문제로 약 1 주일 동안 미친 듯이 나를 몰고 가고 있습니다. 누군가가 해결할 수 있다면이 문제로 정말 고맙겠습니다.
감사합니다.
무슨 일이 일어나는지 확실하지 않지만 재사용을 사용하지 않는 것이 좋습니다. 셀을 사용하여 문제가 다른 곳이 아닌지 확인하십시오. 그냥 "UITableViewCell * cell = nil"을 수행 한 다음 각 셀을 새로운 셀로 만듭니다. 적어도 문제를 일으키는 재사용 가능한 셀이 아님을 확실히 알 수 있습니다. – Srikanth
또한 어디에서 imageButtonTag를 가져오고 있습니까? 최종적으로 button.tag를 설정하고 처음에는 태그가있는 버튼을 얻으려고하는 것 같습니다. 펜촉에 꼬리표를 설정 했습니까? 그런 다음 코드에서 덮어 쓰지 마십시오. – Srikanth
셀의 펜촉에 태그를 설정했습니다. 저는 버튼 1 2와 3을 가지고 있습니다.하지만 나중에 버튼을 클릭하면 버튼을 눌렀을 가능성이 있으므로 버튼을 눌렀는지 알고 싶을 때 변경해야합니다. 나는 그것을 할 수있는 몇 가지 방법이 있다는 것을 압니다. 그러나 이것이 제가 선택한 것입니다. 코드에서 다시 설정하는 것이 귀찮다고 생각하십니까? –