How to create button in the cell of the table view?
I ha开发者_如何学运维ve a table view, the data for the table view is been added in the array. The array is having 5 data. I wanted to create a cell after the fifth row and create a button in that cell. (Like load more options in the app stores applications list)
Thanks in advance.
Try this
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];
}
also you should return 6 in numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 6;
}
Use this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( 0 , 0, 50, 20 );
// add some other button properties here
cell.accessoryView = button;
[button release];
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];
- (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;
}
精彩评论