Ios 在UITableViewCell中放置UIButton时,标题标签已断开
我有自定义的Ios 在UITableViewCell中放置UIButton时,标题标签已断开,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我有自定义的UITableViewCell的UITableView。自定义单元格包含ui按钮和ui标签 在这里,我观察到ui标签文本如我预期的那样发生了更改,但ui按钮文本没有更改 当我在屏幕外滚动按钮时,更改UIButton的标签 为什么它不起作用?我已经使用了Xcode 6和下面的代码 ViewController.h #import <UIKit/UIKit.h> #import "customTableViewCell.h" @interface ViewControll
UITableViewCell
的UITableView
。自定义单元格包含ui按钮
和ui标签
在这里,我观察到ui标签
文本如我预期的那样发生了更改,但ui按钮
文本没有更改
当我在屏幕外滚动按钮时,更改UIButton的标签
为什么它不起作用?我已经使用了Xcode 6和下面的代码
ViewController.h
#import <UIKit/UIKit.h>
#import "customTableViewCell.h"
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
customeTableViewCell.h
#import <UIKit/UIKit.h>
@interface customTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
#导入
@接口customTableViewCell:UITableViewCell
@属性(弱,非原子)IBUIButton*按钮;
@属性(弱,非原子)IBUILabel*标签;
@结束
使用此
[cell.button setTitle:@"MyNewTitle" forState:UIControlStateNormal];
这是因为类具有多个状态(正常、选定、高亮显示、禁用)。当您更改其内部UILabel
(textlab
属性UIButton
)的文本属性时,其属性将被setState
函数覆盖,该函数在加载表时调用
要更改按钮标签中的文本,需要调用setTitle:forState:
方法。以下是您的代码,已修复,可以正常工作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath];
[cell.button setTitle:@"Now it works" forState:UIControlStateNormal];
cell.label.text = @"change label";
return cell;
}
为了完整起见,您还可以将
setAttributedTitle:forState:
方法与NSAttributedString
一起使用,这样您就可以将自己的特定格式字符串设置为标题了。谢谢,它起到了作用。谢谢你的详细解释。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
customTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"customTableViewCell" forIndexPath:indexPath];
[cell.button setTitle:@"Now it works" forState:UIControlStateNormal];
cell.label.text = @"change label";
return cell;
}