IOS--如何在UITableViewCell中移动UILabel?
我试图创建一个(不是很)自定义的UITableView,在单元格的左侧有一个按钮,在按钮的右侧有文本 我试图计算作为单元格一部分的UILabel的放置位置,但更改帧没有任何效果(而且它看起来甚至没有被计算出来——都是零) 我的问题是:何时计算标签的帧大小?(以使更改产生效果) 这样做对吗 下面是代码片段。 第一个来自cell init(为响应出列请求而调用)。。。 是的,'buttonColor'、'onTapSel'、'ButtonCrame'和'buttonColor'是“成员变量(文件静态全局变量)” 现在从CellForRowatineXpath:IOS--如何在UITableViewCell中移动UILabel?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我试图创建一个(不是很)自定义的UITableView,在单元格的左侧有一个按钮,在按钮的右侧有文本 我试图计算作为单元格一部分的UILabel的放置位置,但更改帧没有任何效果(而且它看起来甚至没有被计算出来——都是零) 我的问题是:何时计算标签的帧大小?(以使更改产生效果) 这样做对吗 下面是代码片段。 第一个来自cell init(为响应出列请求而调用)。。。 是的,'buttonColor'、'onTapSel'、'ButtonCrame'和'buttonColor'是“成员变量(文件静态
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EGButtonTableCellID";
EGButtonTableCell * cell = (EGButtonTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.cellButton.indexPath = indexPath;
// Configure the cell...
cell.textLabel.text = @"abcdefghijklmnopqrstuvwxyz";
float xx = cell.cellButton.frame.origin.x + cell.cellButton.frame.size.width + 5;
CGRect frame = cell.textLabel.frame;
frame.origin.x += xx;
cell.textLabel.frame = frame;
return cell;
}
结果是按钮出现在单元格的左侧(如预期),但我在标签中看到的第一个字母是“g”,因此前6个字符隐藏在按钮后面(非预期)
当我转储帧中的值时,两种方法中除origin.x之外的所有值都为零
这应该行得通,是吗?不是吗?为什么不行?等等。
非常感谢大家
:bp:在自定义UITableViewCell的
布局子视图
方法中为UILabel和UIButton设置框架
请参阅有关此主题的Apple文档:
子类可以根据需要重写此方法以执行更精确的操作
其子视图的布局。仅当
子视图的自动调整大小和基于约束的行为不会
提供您想要的行为。您可以使用您的实现来设置
直接显示子视图的框架矩形
在自定义UITableViewCell的
layoutSubviews
方法中为UILabel和UIButton设置框架
请参阅有关此主题的Apple文档:
子类可以根据需要重写此方法以执行更精确的操作
其子视图的布局。仅当
子视图的自动调整大小和基于约束的行为不会
提供您想要的行为。您可以使用您的实现来设置
直接显示子视图的框架矩形
您可以创建自定义UITableViewCell、访问类中的对象或实现方法 CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property (nonatomic, retain) UILabel *customCellLabel;
@property (nonatomic, retain) UIButton *customCellButton;
@end
CustomTableView.m
/* --- */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.customLabel setText:@"hello"];
// [cell.
[cell.customButton addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
}
/* --- */
您可以创建自定义UITableViewCell、访问类中的对象或实现方法 CustomCell.h
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property (nonatomic, retain) UILabel *customCellLabel;
@property (nonatomic, retain) UIButton *customCellButton;
@end
CustomTableView.m
/* --- */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.customLabel setText:@"hello"];
// [cell.
[cell.customButton addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
}
/* --- */
你在哪里设置buttonFrame的值?试图在
cellForRow…
中创建自定义单元格是一件非常痛苦的事情。我知道一开始似乎比较容易,但你应该总是将UITableViewCell子类化,然后这样做。(按照Maggie的回答。)你在哪里设置buttonFrame的值?试图在cellForRow中创建自定义单元格…
是一件非常痛苦的事情。我知道这一点一开始似乎很容易,但你应该始终将UITableViewCell子类化,并以这种方式进行设置。(按照Maggie的回答)重要:请务必调用[超级布局子视图]
作为实现的第一行。重要提示:确保调用[super layoutSubviews]
作为实现的第一行。