2010-09-16 64 views
0

我想用复选框创建一个选项列表。我正在使用UITableView并将自定义类型按钮添加到表格中作为复选框。UITableView中的复选框

我给cellForRowAtIndexPath方法代码粘贴下面:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
CGRect a=CGRectMake(0, 0, 310, 32); 
UIImageView *aImg=[[UIImageView alloc] initWithFrame:a]; 
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a]; 
aImg.image=[[[Resources getResources] getImageLoader] getMainMenuUnselectedImage]; 
bImg.image=[[[Resources getResources] getImageLoader] getLiveScoreSmallBg]; 
[aImg setContentMode:UIViewContentModeScaleToFill]; 
[bImg setContentMode:UIViewContentModeScaleToFill]; 
cell.backgroundView = [[UIView alloc] initWithFrame:a]; 
[cell.backgroundView addSubview:aImg]; 
cell.selectedBackgroundView=bImg; 
cell.font = [UIFont boldSystemFontOfSize:16]; 
cell.selectedTextColor = [UIColor blackColor]; 
UIImage *checked = [UIImage imageNamed:@"check_filled.png"]; 
UIImage *unchecked = [UIImage imageNamed:@"check_empty.png"]; 
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(3, 3, 30, 30)]; 
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
[button setImage: [UIImage imageNamed:@"check_filled.png"] forState: UIControlStateSelected]; 
[button setImage: [UIImage imageNamed:@"check_empty.png"] forState: UIControlStateNormal]; 
[button setEnabled:YES]; 
button.tag = indexPath.row; 
[button setUserInteractionEnabled:YES]; 
[cell.contentView addSubview:button]; 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 3, 250, 17)]; 
label.font = [UIFont boldSystemFontOfSize:15]; 
label.backgroundColor = [UIColor clearColor]; 
label.text = [teamsList objectAtIndex:indexPath.row]; 
[cell.contentView addSubview:label]; 

    [button release]; 
[label release]; 
[aImg release]; 
[bImg release]; 

return cell; 
} 

的问题是,当我运行该程序时,会出现在屏幕没有扣子。另外,当我点击任何行时,程序崩溃。

+0

如何使你的buttonpressed方法定义? – Vladimir 2010-09-16 11:07:49

+0

- (IBAction)buttonPressed; – Aqueel 2010-09-16 11:17:09

回答

2

里有一些问题,你的代码这可能是你的问题不相关,但仍...

  1. 创建单元格内容每次细胞被重用 - 因此,如果您滚动您的表背重复使用的单元格将包含多个相同的子视图 - 这不是你想要的。要固定,你必须移动创建内容的创建单元块:

    if (cell == nil) { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
        // Create cell contents here! 
        ... 
    } 
    ... 
    
  2. 你不能释放你的按钮 - 因为你用简便方法按钮来创建它会被自动释放

    button = [[UIButton buttonWithType:UIButtonTypeCustom] 
            initWithFrame:CGRectMake(3, 3, 30, 30)]; 
    
  3. 这条线可能也会导致问题:

    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
    

    在此处设置选择器不接收任何参数。如果你申报的方法buttonPressed是(最常见的方式)

    - (void) buttonPressed:(id)sender; 
    

    然后,你必须创建选择为@selector(buttonPressed :)表明,它实际上需要1个参数

+0

非常感谢快速和如此有用的回应。我通过删除按钮的释放命令并将IBAction更改为void来进行更改。 – Aqueel 2010-09-16 11:21:36

+0

非常感谢这么快速和有用的回应。我通过删除按钮的释放并将IBAction更改为void来进行更改。代码现在工作正常。 – Aqueel 2010-09-16 11:23:04

+0

所以额外的发布一定是这个问题。 IBAction实际上与void相同,所以它不应该有任何区别。不过,我强烈建议修复我在第一条评论中指出的内容 - 如果您的表是可滚动的并且数据量较大,那么性能和一般外观可能会最终降级 – Vladimir 2010-09-16 11:37:17

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 
int i=indexPath.row; 


ibutton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[ibutton setTitle:@"i" forState:UIControlStateNormal]; 

ibutton.frame = CGRectMake(430.0,40.0,100.0,60.0); 
[cell.contentView addSubview:ibutton]; 
[ibutton setTag:indexPath.row]; 
NSLog(@"the button tag is %d",ibutton.tag); 
[ibutton addTarget:self action:@selector(detailSubView:) forControlEvents:UIControlEventTouchUpInside]; 
addbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[addbutton setTitle:@"add" forState:UIControlStateNormal]; 
addbutton.frame=CGRectMake(560, 40, 100, 60); 
[addbutton setTag:indexPath.row]; 
[cell.contentView addSubview:addbutton]; 
[addbutton addTarget:self action:@selector(addclicked:) forControlEvents:UIControlEventTouchUpInside]; 

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(350.0, 60.0, 50.0, 25.0)]; 
[label setBackgroundColor:[UIColor whiteColor]]; 
[email protected]"2.99$"; 
label.font = [UIFont boldSystemFontOfSize:18]; 
[cell.contentView addSubview:label]; 
[label release]; 

[cell.contentView addSubview:lab]; 
return cell; 
} 


-(IBAction)detailSubView:(id)sender{ 


NSLog(@"working in detail view"); 
UIButton *button = (UIButton *)sender; 

NSLog(@"%d",button.tag); 
a=button.tag; 
NSLog(@"%d",a); 
    category1* B2 = [[category1 alloc] init]; 
    UINavigationController *B2Nav = [[UINavigationController alloc] initWithRootViewController:B2]; 
    B2Nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentModalViewController:B2Nav animated:YES]; 
    [B2 release]; 
    [B2Nav release]; 
} 


-(IBAction)addclicked:(id)sender{ 



    UIButton *button = (UIButton *)sender; 

NSLog(@"%d",button.tag); 
a=button.tag; 
} 
+0

下次请解释你的代码。 – 2012-10-27 07:21:29