我正在尝试使用复选框创建选项列表。我正在使用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 :(得分:2)
您的代码存在一些问题,可能与您的问题无关,但仍然......
每次重复使用单元格时都会创建单元格内容 - 因此,如果您来回滚动表格,重复使用的单元格将包含多个相同的子视图 - 这不是您想要的。要修复您必须将创建内容移动到创建单元格块:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Create cell contents here!
...
}
...
你不能释放你的按钮 - 因为你用方便法创建它按钮是自动释放的
button = [[UIButton buttonWithType:UIButtonTypeCustom]
initWithFrame:CGRectMake(3, 3, 30, 30)];
此行也可能导致问题:
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
在此设置不接收参数的选择器。如果你声明的buttonPressed方法是(最常见的方式)
- (void) buttonPressed:(id)sender;
然后你必须创建选择器为@selector(buttonPressed :)以表明它实际上需要1个参数
答案 1 :(得分: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]];
label.text=@"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;
}