我在UITableView中以编程方式创建了两个按钮,即编辑删除
当我们点击单元格时会显示这些按钮,但是当我尝试单击编辑或删除按钮时,它不会调用适当的方法,即编辑或删除。 这是我的代码。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
edit=[[UIButton alloc]init];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 100, 100, 20)];
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
delete=[[UIButton alloc]init];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(150, 100, 100, 20)];
[delete setTag:2];
[delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
return cell;
}
我的编辑和删除功能非常简单
-(void)edit{
NSLog("%@",selectedValue);
}
-(void)deleteBtn{
NSLog("%@",selectedValue);
}
我已经检查过这个函数应用断点但是没有调用它。
这是 selectedValue 的来法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedValue=[firstName objectAtInder:indexPath.row];
}
先谢谢, 阿伦。
答案 0 :(得分:2)
请您的代码如下
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
edit=[[UIButton alloc]init];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 5, 100, 20)];
[edit setTag:indexPath.row];
[edit addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:edit];
delete=[[UIButton alloc]init];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(210, 5, 100, 20)];
[delete setTag:indexPath.row];
[delete addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:delete];
return cell;
}
使用以下方法
-(IBAction)edit:(id)sender
{
int tag = [sender tag];
NSString *str = [firstName objectAtIndex:tag];
NSLog("%@",str);
}
-(IBAction)deleteBtn:(id)sender
{
int tag = [sender tag];
NSString *str = [firstName objectAtIndex:tag];
NSLog("%@",str);
}
答案 1 :(得分:2)
请注意,问题出在UIButton
框架上。
在你的代码中,按钮的origin.y
都是100.0,所以它超出了单元格的界限(默认高度为44.0)。
像这样更改你的两个UIButton
框架,它对我有效。
UIButton * edit =[[UIButton alloc]init];
[edit setBackgroundColor:[UIColor blackColor]];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
**[edit setFrame:CGRectMake(100,5.0,100, 30)];**
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
UIButton* delete=[[UIButton alloc]init];
[delete setBackgroundColor:[UIColor blackColor]];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
**[delete setFrame:CGRectMake(200,5.0,100,30)];**
[delete setTag:2];
[delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
答案 2 :(得分:1)
您的按钮被反复添加到单元格中,因为每次调用CFRAIP时都会创建它们。将它们移动到cell == nil
块:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
edit=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 100, 100, 20)];
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
delete=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(150, 100, 100, 20)];
[delete setTag:2];
[delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];
}
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
return cell;
}
答案 3 :(得分:1)
问题出在setFrame
表格单元格的高度小于您为按钮设置的坐标。 将代码更改为
[edit setFrame:CGRectMake(100, 5, 100, 20)];
这将有效。
或
只需使用
增加表格行的height
,默认情况下为44
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
答案 4 :(得分:0)
请尝试使用这个....并为删除按钮做同样的事情..........
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// -------------------- Add button to cell --------------
edit = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 20)];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:edit];
}
答案 5 :(得分:0)
请更改按钮名称,因为编辑和删除已在Objective C中定义了关键字
答案 6 :(得分:0)
我可以尝试自定义单元格,您可以在自定义单元格视图中添加按钮并实现下面的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [self.tbl dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//****cell recent deal buy button click goes to login or check out pageview
//[cell.btn setTag:indexPath.row];
//[cell.btn addTarget:self action:@selector(buybutton_checkout:)
// forControlEvents:UIControlEventTouchDown];
// cell.btn.hidden=NO;
NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"icel" owner:nil options:nil];
for (UIView *view in views)
{
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (icel*)view;
}
}
}
//tbl.layer.cornerRadius = 3.9;
//[tbl setClipsToBounds:YES];
[cell.activity startAnimating];
[cell.btn setTitle:_BUYNOW_BTN forState:UIControlStateNormal];
[cell.btn setTag:indexPath.row];
[cell.btn addTarget:self action:@selector(Buy_btnlck:)
forControlEvents:UIControlEventTouchDown];
//cell.btn.hidden=NO;
cell.title_lbl.text=[[_today_similardeal_title_ary objectAtIndex:indexPath.row]capitalizedString];
index_tbl=indexPath.row;
return cell;
}
}
-(IBAction)Buy_btnlck:(UIButton *)button
{
NSInteger intvalue=[[NSString stringWithFormat:@"%ld",(long int)[button tag]]intValue];
}
答案 7 :(得分:0)
像这样修改你的代码......
替换以下两行...
edit=[[UIButton alloc] init];
delete=[[UIButton alloc] init];
像这样......
UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *delete=[UIButton buttonWithType:UIButtonTypeRoundedRect]];
答案 8 :(得分:0)
请勿使用[UIButton alloc] init]
。请改用此
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(edit:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Edit" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 21.0, 40.0, 40.0);
[view addSubview:button];
视图是将按钮添加为子视图的视图。您也可以根据自己的要求设置Button框架。
答案 9 :(得分:0)
试试这个::
表方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configure the cell.
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell= [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;
UIButton *btnEdit=[UIButton buttonWithType:UIButtonTypeCustom];
[btnEdit setTitle:@"Edit" forState:UIControlStateNormal];
[btnEdit setFrame:CGRectMake(100, 100, 100, 20)];
[btnEdit setTag:indexPath.row];
[btnEdit addTarget:self action:@selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnEdit];
UIButton *btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
[btnDelete setTitle:@"Delete" forState:UIControlStateNormal];
[btnDelete setFrame:CGRectMake(150, 100, 100, 20)];
[btnDelete setTag:indexPath.row];
[btnDelete addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnDelete];
}
return cell;
}
然后,按钮操作方法
-(IBAction)clickEdit:(id)sender
{
NSString *str = [firstName objectAtIndex:[sender tag]];
NSLog("Name :: %@",str);
}
-(IBAction)clickDelete:(id)sender
{
NSString *str = [firstName objectAtIndex:[sender tag]];
NSLog("Name :: %@",str);
}
希望它会帮助你。
感谢。