我已经实现了这个代码,它显示了两个标签,但没有进入单元格的按钮告诉我问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ImageOnRightCell";
UILabel *mainLabel, *secondLabel;
UIButton *buttonMe;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentRight;
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 220.0, 25.0)];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:12.0];
secondLabel.textAlignment = UITextAlignmentRight;
secondLabel.textColor = [UIColor darkGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
buttonMe = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
buttonMe.tag = PHOTO_TAG;
buttonMe.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:buttonMe];
}
else {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
buttonMe = (UIButton *)[cell.contentView viewWithTag:PHOTO_TAG];
}
mainLabel.text = @"main text";
secondLabel.text = @"Secon text";
buttonMe.titleLabel.text=@"oh";
return cell;
}
请为此提供帮助
答案 0 :(得分:0)
以编程方式创建按钮使用此代码
UIButton *buttonMe = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonMe addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[buttonMe setTitle:@"Show View" forState:UIControlStateNormal];
buttonMe.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
buttonMe.tag = PHOTO_TAG;
buttonMe.frame = CGRectMake(260.0, 7.0, 30.0, 30.0);
[cell.contentView addSubview:buttonMe];
注意我也包含了设置操作的代码。当你点击它时会尝试方法aMethod:
,所以也要定义它,否则按钮点击事件会出现崩溃
答案 1 :(得分:0)
试试这个,为buttonMe设置backgroundColor:buttonMe.backgroundColor = [UIColor grayColor]; 并将“buttonMe.titleLabel.text = @”哦“”更改为“[buttonMe setTitle:@”oh“forState:UIControlStateNormal];”
答案 2 :(得分:0)
我认为问题在于
buttonMe = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
它会返回一个UIButtonTypeCustom
类型的UIButton,它默认为透明/透明色。
因此可能会在单元格上添加但不可见,请尝试更改按钮的背景颜色并检查其是否仍然不可见。