【发布时间】:2015-12-11 19:34:20
【问题描述】:
所以我在 UITableView 的 CustomCell 类中的 UILabel 下有 UIButtons。我得到了要显示的标签,但似乎无法显示按钮。我为自定义单元格中的所有 3 个 UI 元素设置了全方位的约束。
我还记录了每个按钮的框架,它们似乎在我的单元格的框架内。
这是我用来设置每个单元格的代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_questions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
cell.questionLabel.text = _questions[indexPath.row];
cell.questionLabel.numberOfLines = 0;
cell.disagreeButton.tag = indexPath.row;
cell.agreeButton.tag = indexPath.row + 60;
[cell.contentView addSubview:[cell.disagreeButton viewWithTag:indexPath.row]];
[cell.contentView addSubview:[cell.agreeButton viewWithTag:indexPath.row + 60]];
[cell.disagreeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.agreeButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"Button 1 Frame: %@\n", cell.disagreeButton);
NSLog(@"Button 2 Frame: %@\n", cell.agreeButton);
NSLog(@"Cell Frame: %@\n", cell);
return cell;
}
- (void)buttonClicked:(id)sender {
UIButton *senderButton = (UIButton *)sender;
if (senderButton.tag < 60) {
NSLog(@"Disagree Selected\n");
} else {
NSLog(@"Agree Selected\n");
}
}
我的 CustomCell.h
#import "UIKit/UIKit.h"
@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel* questionLabel;
@property (weak, nonatomic) IBOutlet UIButton* disagreeButton;
@property (weak, nonatomic) IBOutlet UIButton* agreeButton;
@end
【问题讨论】:
标签: ios objective-c uitableview uibutton cell