我想用复选框创建一个选项列表。我正在使用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;
}
的问题是,当我运行该程序时,会出现在屏幕没有扣子。另外,当我点击任何行时,程序崩溃。
如何使你的buttonpressed方法定义? – Vladimir 2010-09-16 11:07:49
- (IBAction)buttonPressed; – Aqueel 2010-09-16 11:17:09