테이블뷰의 셀을 커스터마이징하여


셀에 버튼을 넣고, 그 버튼을 클릭하는 것을 구현하려고 한다


참고할만한 내용들을 검색해서 모아둔다





http://iphonedevsdk.com/forum/iphone-sdk-development/42026-fire-button-click-uitableviewcell.html





구현하려고 했으나 실패한 내용


  1. - (IBAction) button_Clicked:(id)sender
  2. {
  3. UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
  4. UITableView *table=(UITableView*)[cell superview];
  5. NSIndexPath *path=[table indexPathForCell:cell];
  6. NSLog(@been pressed %d si %d,path.section, path.row);
  7. }





상세구현 내용



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

CGRect frame;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {

cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];

frame.origin.x = 10;
frame.size.width = 35;
frame.origin.y=18;
frame.size.height=40;
cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.frame = frame;
cellButton.tag=indexPath.row;
cellButton.backgroundColor=[UIColor grayColor];
[cellButton addTarget:self action:@selector(buttonMethod:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellButton];
}
return cell;

}
// action method
-(void)buttonMethod:(id)sender
{
// this method need to show 2 more buttons on this row and replace the old button(i.e cellbutton) with this seleted one on the same corresponding row.

}




You can put the [sender superview] until you reach your cell and from your cell have an index or something like that to uniquely identify it and do certain operations or solution 2 add a .tag property to the button to identify it or solution 3, forget about the custom class and build your cell dinamically in the cellForRowAtIndexPath method and point the selector to something from the same class as the cellForRowAtIndexPath method.





상세구현 내용


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

{

.

.

.

 btw = (UIButton*)[cell viewWithTag:2];

    [btw setTitle:mData.mDate forState:UIControlContentHorizontalAlignmentCenter];

    [btw addTarget:self action:@selector(btnActionChange:) forControlEvents:UIControlEventTouchUpInside];

.

.

.

}

-(void)btnActionChange:(id)sender

{

    NSIndexPath *indexPath = [self.tableViewList indexPathForCell:(UITableViewCell *)[[sender superviewsuperview]];


    NSLog(@"%d",indexPath.row);

    //여기부분에서 버튼의 타이틀 값을 뽑아서 이용하려고 하는데요. 

    //어떻게 제가 누른 셀안의 버튼의 값을 가져올 수 있을까요?

}




답변1 -> 


UIButton *btn = (UIButton *)sender;

NSString *strTitle = [btn titleForState:UIControlStateNormal];



답변2 ->

가장 편한건 tag긴 한데 예전에 맥부기에서 배운건데
[cell.button addTarget:self action:@selector(clcikCell:event:) forControlEvents:UIControlEventTouchUpInside];


-(void) clcikCell:(id) sender event:(id)event
{
    CGPoint touchPosition = [[[event allTouches] anyObject] locationInView:self.tableview];
    NSIndexPath *indexPath = [self.productTable indexPathForRowAtPoint:touchPosition];
}
이런식으로 구하는 방법이 있지요

Posted by 스타켄지니어
,