按钮在UITableViewCell中不可见时外屏
问题描述:
刷卡我做了一个自定义的UITableViewCell
工作正常,除了当我滑动屏幕外的单元中的按钮变为不可见的事实:按钮在UITableViewCell中不可见时外屏
。
当我切换到其他视图并更改回,按钮再次可见:
。
该按钮仍然存在,可以点击调用该过程,但文本似乎成为零。 任何想法为什么?
的代码:
SettingsViewController.h
#import <UIKit/UIKit.h>
@interface SettingsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *settingsTable;
@end
SettingsViewController.m
#import "SettingsViewController.h"
#import "MyCellClass.h"
@interface SettingsViewController()
@end
@implementation SettingsViewController
{
NSMutableArray *_tableSectionNames;
NSMutableArray *_cellGroups;
NSMutableArray *_cellGroup;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_cellGroups = [[NSMutableArray alloc] init];
_cellGroup = [[NSMutableArray alloc] init];
_tableSectionNames = [[NSMutableArray alloc] initWithObjects:@"Login", nil];
MyCellClass *myCell = [[self settingsTable] dequeueReusableCellWithIdentifier:@"myCell"];
[myCell.textLabel setText:[NSString stringWithFormat:@"Logged in as 1"]];
[myCell.button setTitle:@"Logout" forState:UIControlStateNormal];
[myCell.button addTarget:self action:@selector(userLogout) forControlEvents:UIControlEventTouchUpInside];
[_cellGroup addObject:myCell];
[_cellGroups addObject:_cellGroup];
[[self settingsTable] reloadData];
}
#pragma mark - Settings table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_cellGroups[section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_cellGroups count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [_cellGroups[indexPath.section] objectAtIndex:indexPath.row];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (_tableSectionNames[section])
{
return _tableSectionNames[section];
} else
{
return @"";
}
}
MyCellClass.h
#import <UIKit/UIKit.h>
@interface MyCellClass : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
MyCellClass.m
#import "MyCellClass.h"
@implementation MyCellClass
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
这是当我看到我的自定义单元格与按钮:
<MyCellClass: 0xa7cf1b0; baseClass = UITableViewCell; frame = (0 55; 320 44); text = 'Logged in as 1'; autoresize = W; layer = <CALayer: 0xa7cf380>>
| <UITableViewCellScrollView: 0xa7bc3c0; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0xa7bc650>; layer = <CALayer: 0xa7bc590>; contentOffset: {0, 0}>
| | <UITableViewCellContentView: 0xa7cf470; frame = (0 0; 320 44); opaque = NO; gestureRecognizers = <NSArray: 0xa7be530>; layer = <CALayer: 0xa7bbdf0>>
| | | <UIButton: 0xa7bbe20; frame = (217 5; 83 33); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0xa7bbf10>>
| | | <UILabel: 0xa7bbf40; frame = (15 17; 199 21); text = 'Logged in as 1'; clipsToBounds = YES; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa772f80>>
这是当我看到标准的UITableViewCell无按钮后刷卡:
<MyCellClass: 0xa7cf1b0; baseClass = UITableViewCell; frame = (0 55; 320 44); text = 'Logged in as 1'; autoresize = W; layer = <CALayer: 0xa7cf380>>
| <UITableViewCellScrollView: 0xa7bc3c0; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0xa7bc650>; layer = <CALayer: 0xa7bc590>; contentOffset: {0, 0}>
| | <UITableViewCellContentView: 0xa7cf470; frame = (0 0; 320 43.5); opaque = NO; gestureRecognizers = <NSArray: 0xa7be530>; layer = <CALayer: 0xa7bbdf0>>
| | | <UIButton: 0xa7bbe20; frame = (217 5; 83 33); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0xa7bbf10>>
| | | | <UIButtonLabel: 0xa897150; frame = (29 6; 54 21); text = 'Logout'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa896860>>
| | | <UILabel: 0xa7bbf40; frame = (15 17; 199 21); text = 'Logged in as 1'; clipsToBounds = YES; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xa772f80>>
| | <_UITableViewCellSeparatorView: 0xa88e8b0; frame = (0 43.5; 320 0.5); layer = <CALayer: 0xa8717d0>>
| | <_UITableViewCellSeparatorView: 0xa8780b0; frame = (0 0; 320 0.5); layer = <CALayer: 0xa882550>>
答
原因w作为设置我的自定义单元格的@properties为“弱”,而不是“强”:
@interface MyCustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *buttonRight;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
所以我改成了“强”:
@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *buttonRight;
@property (strong, nonatomic) IBOutlet UILabel *textLabel;
@end
然后,我看到两个文本重叠彼此。原因是我将文本标签命名为textLabel
,它与UITableViewCell
中的文本标签名称相同。所以我基本上用相同的文本填充了两个文本标签,我注意到只有按钮消失,但实际上它是整个定制单元类消失。
所以我也改变了属性的名称,它工作得很好:
@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *buttonRight;
@property (strong, nonatomic) IBOutlet UILabel *labelLeft;
@end
显示代码为你的表委托/数据源的方法 – Wain
感谢北斗星,添加的代码。 – Manuel
那么在MyCellClass中,由于表代码并没有做任何事情。 (另外,你什么时候创建'_cell *'数组 - 我只是没有显示?) – Wain