標籤:style color get os cti for
在自訂tableView中,為cell添加button點擊事件後,如何擷取其對應的序號?
1、建立tableView:
先建立一個成員變數:
@interface MyCameraViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *_tableView;
}@end
在viewDidLoad中初始化
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, 320, [UIScreen mainScreen].bounds.size.height-65) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_tableView];
2、實現其必須實現的代理方法
#pragma mark - Table view data source
//組數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
//cell的個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 10;
}
3、建立cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
cell.textLable.text = @"hello,你好";
UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
CGRect frame = CGRectMake( 0.0 , 0.0 , 30 , 24 );
button.frame = frame;
// [button setImage:image forState:UIControlStateNormal ]; //可以給button設定圖片
// button.backgroundColor = [UIColor clearColor ];
[button addTarget:self action:@selector(accessoryButtonTappedAction:) forControlEvents:UIControlEventTouchUpInside];
//將該button賦給cell屬性accessoryView
cell. accessoryView = button;
}
return cell;
}
//實現button的點擊事件
- (void)accessoryButtonTappedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell;
if (iOS7) {
cell = (UITableViewCell *)button.superview.superview;
}else
{
cell = (UITableViewCell *)button.superview;
}
int row = [_tableView indexPathForCell:cell].row; //row為該button所在的序號
}