如何自定义tableviewcell中的按钮?
How to customize a button in tableviewcell?
我正在开发一个 iOS 应用程序,我正在使用 Xcode 6.
我在 tableview 单元格中创建了 5 个按钮 programatically.i 只想自定义一个按钮 time.That 意味着当我单击一个按钮时它必须放大,其他按钮将保持默认大小。
现在的问题是当我点击一个按钮时,它会放大,如果我点击另一个按钮,它也会放大 enlarge.But我想将其余按钮保持在我在 CGRect 函数中输入的默认大小.
这是我的代码:
#import "ViewController.h"
@interface ViewController ()
{
UITableViewCell *cell;
UIButton *b2,*b1;
}
//@property(strong,nonatomic)UIButton *b3,*b4,*b5;
//@property(strong,nonatomic)UIButton *b1,*b2;
@end
@implementation ViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UIButton *b1=[UIButton buttonWithType:UIButtonTypeCustom];
b1.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
[b1 setTag:1];
[b1 addTarget:self action:@selector(modify:) forControlEvents:UIControlEventTouchUpInside];
[b1 setBackgroundColor:[UIColor blueColor]];
[b1 setTitle:@"Blue" forState:UIControlStateNormal];
UIBButton *b2=[UIButton buttonWithType:UIButtonTypeCustom];
b2.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
[b2 setTag:2];
[b2 addTarget:self action:@selector(modify1:) forControlEvents:UIControlEventTouchUpInside];
[b2 setBackgroundColor:[UIColor redColor]];
[b2 setTitle:@"red" forState:UIControlStateNormal];
UIButton *b3=[UIButton buttonWithType:UIButtonTypeCustom];
b3.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
[b3 setTag:3];
[b3 addTarget:self action:@selector(modify2:) forControlEvents:UIControlEventTouchUpInside];
[b3 setBackgroundColor:[UIColor greenColor]];
[b3 setTitle:@"Green" forState:UIControlStateNormal];
UIButton *b4=[UIButton buttonWithType:UIButtonTypeCustom];
b4.frame=CGRectMake(193.0, 40.0,40.0, 40.0);
[b4 setTag:4];
[b4 addTarget:self action:@selector(modify3:) forControlEvents:UIControlEventTouchUpInside];
[b4 setBackgroundColor:[UIColor blackColor]];
[b4 setTitle:@"Black" forState:UIControlStateNormal];
UIButton *b5=[UIButton buttonWithType:UIButtonTypeCustom];
b5.frame=CGRectMake(253.0, 40.0, 40.0, 40.0);
[b5 setTag:5];
[b5 addTarget:self action:@selector(modify4:) forControlEvents:UIControlEventTouchUpInside];
[b5 setBackgroundColor:[UIColor orangeColor]];
[b5 setTitle:@"Orange" forState:UIControlStateNormal];
[cell addSubview:b1];
[cell addSubview:b2];
[cell addSubview:b3];
[cell addSubview:b4];
[cell addSubview:b5];
return cell;
}
-(void)modify:(UIButton *)btn
{
if (btn.tag==1) {
btn.frame=CGRectMake(13.0, 40.0, 60.0, 60.0);
}
if (btn.tag!=1)
{
btn.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
btn.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
btn.frame=CGRectMake(193.0, 40.0, 40.0, 40.0);
btn.frame=CGRectMake(253.0, 40.0, 40.0, 40.0);
}
}
-(void)modify1:(UIButton *)btn1
{
if (btn1.tag==2) {
btn1.frame=CGRectMake(73.0, 40.0, 60.0, 60.0);
if (btn1.tag==1) {
btn1.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
}
}
-(void)modify2:(UIButton *)btn2
{
if (btn2.tag==3) {
btn2.frame=CGRectMake(133.0, 40.0, 60.0, 60.0);
}
}
-(void)modify3:(UIButton *)btn3
{
if (btn3.tag==4) {
btn3.frame=CGRectMake(193.0, 40.0, 60.0, 60.0);
if (btn3.tag==3) {
btn3.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
}
if (btn3.tag==2) {
btn3.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
}
if (btn3.tag==1) {
btn3.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
}
if (btn3.tag==5) {
btn3.frame=CGRectMake(253.0, 40.0, 40.0, 40.0);
}
}
}
-(void)modify4:(UIButton *)btn4
{
if (btn4.tag==5) {
btn4.frame=CGRectMake(253.0, 40.0, 60.0, 60.0);
if (btn4.tag==4) {
btn4.frame=CGRectMake(193.0, 40.0, 40.0, 40.0);
}
if (btn4.tag==3) {
btn4.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
}
if (btn4.tag==2) {
btn4.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
}
if (btn4.tag==1) {
btn4.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
}
}
}
}
不要做这么多事情,而是做以下更改:
在下面的方法中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
UILabel *lblFirst=nil;
UILabel *lblSecond=nil;
UITableViewCellStyle style =UITableViewCellStyleSubtitle;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier] autorelease];
//Account
lblFirst = [[UILabel alloc] initWithFrame:CGRectInset(CGRectMake(0, 0, 130,
iRowHeight),10,2)] ;
lblFirst.tag=1;
[self labelCommonSettingsLeftAlign:lblFirst];
[cell.contentView addSubview:lblFirst];
//----------------------------------------
//name
lblSecond = [[UILabel alloc] initWithFrame:CGRectInset(CGRectMake(131, 0, 130,
iRowHeight),10,2)] ;
lblSecond.tag=2;
[self labelCommonSettingsLeftAlign:lblSecond];
[cell.contentView addSubview:lblSecond];
[lblFirst release];
lblFirst=nil;
[lblSecond release];
lblSecond=nil;
}
lblFirst = (UILabel *)[cell.contentView viewWithTag:1];
lblSecond = (UILabel *)[cell.contentView viewWithTag:2];
/*//Here u can check the condition of selected tag and indexpath.row.
for e.g if(selectedTag==indexpath.row)
{
//change button size to New Size
}
else
{
//change button size to Old Size
}*/
return cell;
}
现在在上面的代码中,我在单元格上添加了两个标签。而不是你可以在单元格上添加按钮。
现在只要你点击按钮,你就会得到按钮标签。通过选中 cellForRowAtIndexPath
中的 tag value
和单元格 indexpath.row
,您可以更改所选按钮和其他按钮的按钮框架。为此,您需要调用 table 视图 reloadData
函数。确保 selectedIndex 初始化为 -1。
可能这可以帮助您获得正确的索引
-(void)modify4:(UIButton *)sender {
GPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *currentPhotoIndexPath = [self.tableView indexPathForItemAtPoint:buttonPosition];
.....
}
[btn1 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn5 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
btn1.tag=1;
btn2.tag=2;
btn3.tag=3;
btn4.tag=4;
btn5.tag=5;
-(IBAction)increaseAction:(id)sender
{
UIButton *button = (UIButton *) sender;
//Here perform the desired operation
}
我正在开发一个 iOS 应用程序,我正在使用 Xcode 6.
我在 tableview 单元格中创建了 5 个按钮 programatically.i 只想自定义一个按钮 time.That 意味着当我单击一个按钮时它必须放大,其他按钮将保持默认大小。
现在的问题是当我点击一个按钮时,它会放大,如果我点击另一个按钮,它也会放大 enlarge.But我想将其余按钮保持在我在 CGRect 函数中输入的默认大小.
这是我的代码:
#import "ViewController.h"
@interface ViewController ()
{
UITableViewCell *cell;
UIButton *b2,*b1;
}
//@property(strong,nonatomic)UIButton *b3,*b4,*b5;
//@property(strong,nonatomic)UIButton *b1,*b2;
@end
@implementation ViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UIButton *b1=[UIButton buttonWithType:UIButtonTypeCustom];
b1.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
[b1 setTag:1];
[b1 addTarget:self action:@selector(modify:) forControlEvents:UIControlEventTouchUpInside];
[b1 setBackgroundColor:[UIColor blueColor]];
[b1 setTitle:@"Blue" forState:UIControlStateNormal];
UIBButton *b2=[UIButton buttonWithType:UIButtonTypeCustom];
b2.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
[b2 setTag:2];
[b2 addTarget:self action:@selector(modify1:) forControlEvents:UIControlEventTouchUpInside];
[b2 setBackgroundColor:[UIColor redColor]];
[b2 setTitle:@"red" forState:UIControlStateNormal];
UIButton *b3=[UIButton buttonWithType:UIButtonTypeCustom];
b3.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
[b3 setTag:3];
[b3 addTarget:self action:@selector(modify2:) forControlEvents:UIControlEventTouchUpInside];
[b3 setBackgroundColor:[UIColor greenColor]];
[b3 setTitle:@"Green" forState:UIControlStateNormal];
UIButton *b4=[UIButton buttonWithType:UIButtonTypeCustom];
b4.frame=CGRectMake(193.0, 40.0,40.0, 40.0);
[b4 setTag:4];
[b4 addTarget:self action:@selector(modify3:) forControlEvents:UIControlEventTouchUpInside];
[b4 setBackgroundColor:[UIColor blackColor]];
[b4 setTitle:@"Black" forState:UIControlStateNormal];
UIButton *b5=[UIButton buttonWithType:UIButtonTypeCustom];
b5.frame=CGRectMake(253.0, 40.0, 40.0, 40.0);
[b5 setTag:5];
[b5 addTarget:self action:@selector(modify4:) forControlEvents:UIControlEventTouchUpInside];
[b5 setBackgroundColor:[UIColor orangeColor]];
[b5 setTitle:@"Orange" forState:UIControlStateNormal];
[cell addSubview:b1];
[cell addSubview:b2];
[cell addSubview:b3];
[cell addSubview:b4];
[cell addSubview:b5];
return cell;
}
-(void)modify:(UIButton *)btn
{
if (btn.tag==1) {
btn.frame=CGRectMake(13.0, 40.0, 60.0, 60.0);
}
if (btn.tag!=1)
{
btn.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
btn.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
btn.frame=CGRectMake(193.0, 40.0, 40.0, 40.0);
btn.frame=CGRectMake(253.0, 40.0, 40.0, 40.0);
}
}
-(void)modify1:(UIButton *)btn1
{
if (btn1.tag==2) {
btn1.frame=CGRectMake(73.0, 40.0, 60.0, 60.0);
if (btn1.tag==1) {
btn1.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
}
}
-(void)modify2:(UIButton *)btn2
{
if (btn2.tag==3) {
btn2.frame=CGRectMake(133.0, 40.0, 60.0, 60.0);
}
}
-(void)modify3:(UIButton *)btn3
{
if (btn3.tag==4) {
btn3.frame=CGRectMake(193.0, 40.0, 60.0, 60.0);
if (btn3.tag==3) {
btn3.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
}
if (btn3.tag==2) {
btn3.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
}
if (btn3.tag==1) {
btn3.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
}
if (btn3.tag==5) {
btn3.frame=CGRectMake(253.0, 40.0, 40.0, 40.0);
}
}
}
-(void)modify4:(UIButton *)btn4
{
if (btn4.tag==5) {
btn4.frame=CGRectMake(253.0, 40.0, 60.0, 60.0);
if (btn4.tag==4) {
btn4.frame=CGRectMake(193.0, 40.0, 40.0, 40.0);
}
if (btn4.tag==3) {
btn4.frame=CGRectMake(133.0, 40.0, 40.0, 40.0);
}
if (btn4.tag==2) {
btn4.frame=CGRectMake(73.0, 40.0, 40.0, 40.0);
}
if (btn4.tag==1) {
btn4.frame=CGRectMake(13.0, 40.0, 40.0, 40.0);
}
}
}
}
不要做这么多事情,而是做以下更改: 在下面的方法中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
UILabel *lblFirst=nil;
UILabel *lblSecond=nil;
UITableViewCellStyle style =UITableViewCellStyleSubtitle;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier] autorelease];
//Account
lblFirst = [[UILabel alloc] initWithFrame:CGRectInset(CGRectMake(0, 0, 130,
iRowHeight),10,2)] ;
lblFirst.tag=1;
[self labelCommonSettingsLeftAlign:lblFirst];
[cell.contentView addSubview:lblFirst];
//----------------------------------------
//name
lblSecond = [[UILabel alloc] initWithFrame:CGRectInset(CGRectMake(131, 0, 130,
iRowHeight),10,2)] ;
lblSecond.tag=2;
[self labelCommonSettingsLeftAlign:lblSecond];
[cell.contentView addSubview:lblSecond];
[lblFirst release];
lblFirst=nil;
[lblSecond release];
lblSecond=nil;
}
lblFirst = (UILabel *)[cell.contentView viewWithTag:1];
lblSecond = (UILabel *)[cell.contentView viewWithTag:2];
/*//Here u can check the condition of selected tag and indexpath.row.
for e.g if(selectedTag==indexpath.row)
{
//change button size to New Size
}
else
{
//change button size to Old Size
}*/
return cell;
}
现在在上面的代码中,我在单元格上添加了两个标签。而不是你可以在单元格上添加按钮。
现在只要你点击按钮,你就会得到按钮标签。通过选中 cellForRowAtIndexPath
中的 tag value
和单元格 indexpath.row
,您可以更改所选按钮和其他按钮的按钮框架。为此,您需要调用 table 视图 reloadData
函数。确保 selectedIndex 初始化为 -1。
可能这可以帮助您获得正确的索引
-(void)modify4:(UIButton *)sender {
GPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *currentPhotoIndexPath = [self.tableView indexPathForItemAtPoint:buttonPosition];
.....
}
[btn1 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
[btn5 addTarget:self action:@selector(increaseAction:) forControlEvents:UIControlEventTouchUpInside];
btn1.tag=1;
btn2.tag=2;
btn3.tag=3;
btn4.tag=4;
btn5.tag=5;
-(IBAction)increaseAction:(id)sender
{
UIButton *button = (UIButton *) sender;
//Here perform the desired operation
}