如何在表格视图的单元格中创建按钮?

时间:2011-05-10 11:21:20

标签: objective-c uitableview uibutton

我有一个表视图,表视图的数据已添加到数组中。该阵列有5个数据。我想在第五行之后创建一个单元格并在该单元格中创建一个按钮。 (比如在应用商店应用列表中加载更多选项)

提前致谢。

4 个答案:

答案 0 :(得分:3)

试试这个

if(indexpath.row == 5) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( 0 , 0, 50, 20 );
[button addTarget:self action:@selector(deletePostMethod) forControlEvents:UIControlEventTouchUpInside];

// add some other button properties here 
[cell.contentView addSubView:button];
[button release]; 
}

你也应该在numberOfRowsInSection

中返回6
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 6;
}

答案 1 :(得分:0)

使用此:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( 0 , 0, 50, 20 );
// add some other button properties here 
cell.accessoryView = button;
[button release];    

答案 2 :(得分:0)

        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [deleteButton setFrame:CGRectMake(280, 3, 26, 36)];
        deleteButton.contentMode = UIViewContentModeScaleAspectFill;     
        UIImage *newImage12 = [UIImage imageNamed:@"delete1.png"];
        deleteButton.tag = indexPath.row;
        [deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
        [deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];
        [deleteButton addTarget:self action:@selector(deletePostMethod:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:deleteButton];

答案 3 :(得分:0)

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    //for inserting submit button at the end of the section
    UIView *footerView=nil;
    if(section == 4)
    {
    footerView  = [[UIView alloc] init];
    UIButton *btnSubmit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSubmit setFrame:CGRectMake(570, 22, 120, 50)];
    [btnSubmit setTitle:@"Submit" forState:UIControlStateNormal];
    [btnSubmit.titleLabel setFont:[UIFont systemFontOfSize:25]];
    [btnSubmit setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
    UIImage *btnImage=[UIImage imageNamed:@"dark-green.png"];//setting image of button
    [btnSubmit setBackgroundImage:btnImage forState:UIControlStateNormal];
    [footerView addSubview:btnSubmit];
    }
    return footerView;    
}