2012-08-04 6 views
1

내 CustomCell에서 만드는 버튼이 있습니다.UITableViewCell에서 Button을 사용하여 작업을 수행하는 방법?

내가 실행할 때 단추는 TableViewController에서 만드는 UITableView 내 셀에 표시됩니다. 내 셀 안의 onoffBtn을 눌러도되지만 그럴 수는 없습니다. 내 버튼이 작업을 수행 할 수 있도록 어떻게해야합니까? 에서 공정을 위해 내가 작성해야하는 경우

이 내 TableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]]; 
    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = _customCell; 
     _customCell = nil; 
    } 
    // Show my cell 
    return cell; 
} 

및 기타 질문에 대한 내부 내 cellForRowAtIndexPath은 무엇입니까? 내 TableViewController.m 또는 내 CustomCell.m 안에? 마음에 들지 않으면 코드와 설명을 제공해주세요. 여기에 새로운 것이지만 여전히 배우고 있습니다. 나는 당신이 작성한 코드의 모든 라인을 이해하고 싶다. 고맙습니다.

업데이트 :

이 지금까지 TableViewController.m 안에 내 cellForRowAtIndexPath입니다 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]]; 
    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     cell = _customCell; 
     _customCell = nil; 
    } 
    button.tag = indexPath.row; 
    cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row]; 
    cell.timerLabel.text = [timeArray objectAtIndex:indexPath.row]; 
    time = [[secondArray objectAtIndex:indexPath.row] integerValue]; 
    NSLog(@"Title %@, time %@, second %@, time %i, tag %d", titleArray, timeArray, secondArray, time, button.tag); 
    return cell; 
} 

는이이 로그는 말했다 :

2012-08-04 17:38:36.257 Table[1269:f803] Title (
    "Test 1", 
    "Test 2" 
), time (
    "10 secs", 
    "5 secs" 
), second (
    10, 
    5 
), time 10, tag 0 // tag 0 for cell 1 
2012-08-04 17:38:36.261 Table[1269:f803] Title (
    "Test 1", 
    "Test 2" 
), time (
    "10 secs", 
    "5 secs" 
), second (
    10, 
    5 
), time 5, tag 0 // tag should be 1 for cell 2 but it said 0. How to fix it? 

답변

1

이것은 당신이 액션을 가지는 버튼을 추가 방법입니다 셀 있음

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

// ADDING A BUTTON WITH TARGET 
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundRect]; 
btn.frame = CGRectMake(0, 0, 50, 50); 
btn.showsTouchWhenHighlighted = YES; 
btn.tag = indexPath.row; 
[btn addTarget:self action:@selector(onoffBtn:) forControlEvents:UIControlEventTouchUpInside]; 

[cell.contentView addSubview:btn]; 

return cell; 
} 


- (IBAction)onoffBtn:(id)sender 
{ 
UIButton *button = (UIButton *)sender; 
button.selected = !button.selected; 
if (button.selected) 
{ 
    // Do action 1 
} 
else 
{ 
    // Do action 2 
} 
} 
+0

빠른 답변을 주셔서 감사합니다, 메신저 초보자. 내가 뭔가를 요청할 수 있습니다? 내 CustomCell 안에 이미 버튼을 넣었습니다. 코드를 사용하여 버튼을 다시 추가해야합니까? 대단히 감사합니다 – Piyo

+1

버튼을 제거하고이 코드를 추가하면 프레임 세트에 따라 모든 셀에 버튼을 동적으로 추가하거나 셀이 일정하고 xib를 통해 버튼을 추가하면 간단하게 동일한 버튼을 연결할 수 있습니다 IBAction 모든 xB 파일에있는 버튼 – superGokuN

+0

선생님, 나는 이미 그것을 제거하지만 내 코드와 내 버튼을 연결할 수 있습니다 그리고 내가 넣어 button.tag = indexPath.row; 내 cellForRowAtIndexPath 및 NSLog 내부. 하지만 내 tableview 내에서 2 셀을 실행하면 NSLog가 표시 한 두 태그 번호가 모두 0이 아니라 0이됩니다. 내 버튼이 올바른 태그를 설정할 수 있도록 가르쳐주세요. – Piyo

관련 문제