UITableViewCell中的按钮在屏幕外滑动时不可见

时间:2014-02-26 21:57:26

标签: uitableview

我制作的自定义UITableViewCell工作正常,但是当我在屏幕外面滑动按钮时按钮变得不可见这一事实:

button invisble after swiping outside top of screen

当我换到另一个视图并改回来时,该按钮再次可见:

button visible

按钮仍然存在,可以点击以调用该过程,但文本似乎变为零。 知道为什么吗?

代码:

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>>

1 个答案:

答案 0 :(得分:0)

原因是我将自定义单元格的@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