将 UIButton 添加到 UItableview 单元格 [英] Adding UIButton to UItableview cells

查看:42
本文介绍了将 UIButton 添加到 UItableview 单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个部分的分组 tableview,我想为每个部分添加不同的按钮,我在第一部分中有 5 个单元格,我只想在第 2 个、第 3 个和第 4 个单元格中添加 3 个取消按钮.我的代码看起来像这样

i have a grouped tableview which contains 3 sections,i want to add different buttons for each section,,i have 5 cells in the first section,and i want to add only 3 unbuttons in the 2nd ,3r an 4th cell.my code look like this

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return [_arayfirstrow count];
    else if(section == 1)
        return [_arraysecondrow count];
    else  if(section == 2)
        return [_arraythirdrow count];
}
   // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
   // static NSUInteger nTag = 100;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia"size:20.0];



    }

    // Set up the cell...
    if(indexPath.section == 0){
            cell.textLabel.text = [_arayfirstrow objectAtIndex:indexPath.row];
        NSString *imageName = [_arayfirstrowimg objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:imageName];

**//when i add below code for inserting button on the first section ,it successfully adds the button,but i don't need button for 1st and 5th cell..ans also it crashes when i tap the button for 2 or three time**

  CGRect buttonRect = CGRectMake(210, 25, 65, 25);
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = buttonRect;
        // set the button title here if it will always be the same
        [button setTitle:@"Action" forState:UIControlStateNormal];
        button.tag = 1;
        [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView insertSubview:button atIndex:0];



       // [usernamebutton release];

    }else if(indexPath.section == 1){
        cell.textLabel.text = [_arraysecondrow objectAtIndex:indexPath.row];
    }
    else if(indexPath.section == 2)
    {
        cell.textLabel.text = [_arraythirdrow objectAtIndex:indexPath.row];

    }
    return cell;
}

有什么办法可以解决这个问题.提前致谢.

is there any solution to solve this. thanks in advance.

推荐答案

你可以这样试试:

if(indexPath.section == 0)
{
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3)
    {
        CGRect buttonRect = CGRectMake(210, 25, 65, 25);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = buttonRect;
        // set the button title here if it will always be the same
        [button setTitle:@"Action" forState:UIControlStateNormal];
        button.tag = 1;
        [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubView:button atIndex:0];
    }
}

您是否定义了 myAction: 方法?如果没有,它可能会导致您的应用程序崩溃.

Have you defined the method myAction:? If not, it might be crashing your application.

这篇关于将 UIButton 添加到 UItableview 单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆