테이블뷰의 셀을 커스터마이징하여
셀에 버튼을 넣고, 그 버튼을 클릭하는 것을 구현하려고 한다
참고할만한 내용들을 검색해서 모아둔다
http://iphonedevsdk.com/forum/iphone-sdk-development/42026-fire-button-click-uitableviewcell.html
구현하려고 했으나 실패한 내용
- - (IBAction) button_Clicked:(id)sender
- {
- UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
- UITableView *table=(UITableView*)[cell superview];
- NSIndexPath *path=[table indexPathForCell:cell];
- NSLog(@been pressed %d si %d,path.section, path.row);
- }
상세구현 내용
- (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 superview] superview]];
NSLog(@"%d",indexPath.row);
//여기부분에서 버튼의 타이틀 값을 뽑아서 이용하려고 하는데요.
//어떻게 제가 누른 셀안의 버튼의 값을 가져올 수 있을까요?
}
답변1 ->
UIButton *btn = (UIButton *)sender;
답변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];
}
이런식으로 구하는 방법이 있지요
'IT > iPhone' 카테고리의 다른 글
ios에서 타이머에 스레드를 사용하는 방법 _ 검색편 (0) | 2016.05.09 |
---|---|
ios에서 타이머 기능을 추가하기 _ 검색편 (0) | 2016.05.08 |
ios에서 2차원배열 값 replaceObjectAtIndex 사용하여 수정하기 (0) | 2016.05.06 |
ios에서 PickerView를 사용하여, 시간을 선택하기 (0) | 2016.05.04 |
ios에서 테이블뷰에서, 커스터마이징된 셀을 삭제하는 법 - 검색편 (0) | 2016.05.03 |