1

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

- (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;
}

有没有办法解决这个问题。提前致谢。

4

3 回答 3

2

你可以这样尝试:

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:吗?如果没有,它可能会使您的应用程序崩溃。

于 2012-04-19T14:43:22.667 回答
1

首先,这很容易:

将 UITableViewCell 的子类添加到您的项目中,插入标题并在委托方法中添加单元格,如下所示:

static NSString *CellIdentifier = @"SpecialCell";
// static NSUInteger nTag = 100;
YourTableViewCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (specialCell == nil) {
    specialCell = [[[YourTableViewCellClass alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

在您自己的新 Cell 类中添加您喜欢的按钮和视图

[self addSubview:button];

这是您的特殊单元格,即第一部分中您的第 2、第 3 和第 4 个单元格的单元格。

就像你已经做的那样,以与上面相同的方式初始化一个标准单元。现在魔法来了:

if(indexPath.section == 0){
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3){
        //add text to buttons of your specialCell
        //add color...
        return specialCell;
    } else {
        return cell;
    }
}

只是一个小概述,它是如何完成的。希望有帮助。

于 2012-04-19T14:20:20.230 回答
0
if(indexPath.section == 0)
{
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3)
    {    

UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [deletebtn setFrame:CGRectMake(170,5, 25, 25)];
            deletebtn.tag=indexPath.row;
            [deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal];
            [deletebtn addTarget:self action:@selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:deletebtn];
}
}
于 2013-05-17T09:54:06.700 回答