2011-10-12 3 views
0

난 세포를 추가하고 삭제할 수있는 테이블보기가 있습니다. 추가 된 각 셀에 대해 셀의 textLabel에서 1과 1을 더하는 더하기 및 빼기 버튼이 셀의 두 버튼에 표시됩니다. 그러나 1 셀 버튼을 누르면 다른 셀 textLabel에서 잠급니다.테이블보기 셀의 단추가 다른 셀과 섞입니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  
(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier]; 
} 
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];  
cell.textLabel.text = [cells objectAtIndex:indexPath.row]; 

newBtn = [[UIButton alloc]init]; 
newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[newBtn setFrame:CGRectMake(195,20,55,35)]; 
[newBtn addTarget:self action:@selector(subtractLabelText:) 
forControlEvents:UIControlEventTouchUpInside]; 
[newBtn setTitle:@"-" forState:UIControlStateNormal]; 
[cell addSubview:newBtn]; 

return cell; 
} 

이 다음 방법으로이 방법은 내가 빼기 버튼에 매여 하나입니다 : 여기 내 cellForRow 방법이다

- (IBAction)subtractLabelText:(id)sender{ 

if ([[cell.textLabel text] intValue] == 0){ 
[newBtn setEnabled:NO]; 
} 
else{ 
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text 
intValue] -1]; 
    } 
} 

이 도와주세요! 정말 고마워! : D

답변

2

subtractLabelText:의 인수로 셀을 전달해야합니다. 그렇지 않으면이 함수는 액세스하는 셀을 알 수 없습니다.


내가 할 수있는 셀을 정의하는 줄을 추가하는 것입니다 위해 마음에 오는 간단한 일이 :

- (IBAction)subtractLabelText:(id)sender{ 

UITableViewCell *cell = (UITableViewCell*)[sender superview]; // <-- ADD THIS LINE 

if ([[cell.textLabel text] intValue] == 0){ 
[newBtn setEnabled:NO]; 
} 
else{ 
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text 
intValue] -1]; 
    } 
} 
+0

메신저 꽤 noobish 그것은 아, 그리고 코딩에 올 때! 내 세포 이름이 셀이라면 매개 변수를 어떻게 작성해야합니까? 마찬가지로 - (void) subtractLabelText : (UITableViewCell *) cell? 아니면 다른 방법이 있습니까? 정말 고마워!! : D – iProRage

+0

@iProRage 내 업데이트를 참조하십시오. 이 댓글을 다시 게시하는 것을 중지하십시오. – PengOne

+0

완벽하게 작동합니다! 정말 고마워!! : D : D – iProRage

관련 문제