UITableViewCell中的按钮在屏幕外滑动时不可见(Button in UITableViewCell invisible when swiped outside screen)

编程入门 行业动态 更新时间:2024-10-26 22:19:57
UITableViewCell中的按钮在屏幕外滑动时不可见(Button in UITableViewCell invisible when swiped outside screen)

我做了一个自定义的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>>

I made a custom UITableViewCell that works fine, except for the fact that when I swipe the cell outside the screen the button becomes invisible:

.

When I change to another view and change back, the button is visible again:

.

The button is still there and can be tapped to call the procedure, but the text seems to become nil. Any idea why?

The code:

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

This is when I see my custom cell with button:

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

This is when I see the standard UITableViewCell without button after swiping:

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

最满意答案

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

The reason was that i set the @properties of my custom cell as "weak" instead of "strong":

@interface MyCustomCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIButton *buttonRight; @property (weak, nonatomic) IBOutlet UILabel *textLabel; @end

So I changed it to "strong":

@interface MyCustomCell : UITableViewCell @property (strong, nonatomic) IBOutlet UIButton *buttonRight; @property (strong, nonatomic) IBOutlet UILabel *textLabel; @end

Then I saw two texts overlapping each other. The reason for that was that I named the text label textLabel which is the same name as the text label in UITableViewCell. So I basically filled both text labels with the same text and I noticed only the button to disappear, but actually it was the whole custom cell class that disappeared.

So I changed also the property name and it worked fine:

@interface MyCustomCell : UITableViewCell @property (strong, nonatomic) IBOutlet UIButton *buttonRight; @property (strong, nonatomic) IBOutlet UILabel *labelLeft; @end

更多推荐

本文发布于:2023-08-07 08:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   屏幕   UITableViewCell   Button   screen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!