Mar*_*soi 38 iphone uitableview
我是iPhone开发的新手,我目前正在开发一个简单的RSS阅读器应用程序.我遇到的问题是我需要重新定位textLabel内部UITableViewCells.我试过了setFrame,setCenter但它没有做任何事情.有没有人知道我需要在tableView:cellForRowAtIndexPath:方法内部重新定位textLabel单元格顶部(x = 0,y = 0)?
谢谢
PS:UITableViewCell由一个名为的变量引用cell.我试过[cell setFrame:CGRectMake(0, 0, 320, 20)]没有成功.
wal*_*wal 99
您可以为UITableViewCell创建子类并自定义de textLabel框架.看到答案:标签在UITableViewCell中对齐.它对我来说很完美.
这是我的子类
#import "UITableViewCellFixed.h"
@implementation UITableViewCellFixed
- (void) layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, 0, 320, 20);
}
@end
Run Code Online (Sandbox Code Playgroud)
这是我的UITableViewControllerClass:
UITableViewCellFixed *cell = (UITableViewCellFixed *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCellFixed alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*soi 10
似乎我解决了自己的问题.这是一些代码,以防有人遇到同样的问题:
UILabel *ttitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];
ttitle.font = [UIFont boldSystemFontOfSize:13];
ttitle.textColor = [UIColor blackColor];
ttitle.textAlignment = UITextAlignmentLeft;
[ttitle setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
[cell.contentView addSubview:ttitle];
Run Code Online (Sandbox Code Playgroud)
我们的想法是创建自己的标签对象,因为textLabel是自动定位的,无法移动.
干杯.
小智 9
原始海报的代码不起作用的原因是,似乎在从委托方法返回UITableViewCell之后设置了textLabel的框架.
我注意到我可以成功地改变textLabel的许多属性,例如文本对齐,颜色,字体等,但是改变框架没有效果,当我稍后将框架打印到调试器时(如选择),框架不是我设定的.因此,我得出结论,UIKit框架在从委托方法返回后改变了textLabel的框架.毫无疑问,这可能是因为Apple工程师想要确保您的文本被绘制到屏幕上,因此他们会对其进行测量并更改框架以使其适合.他们可能认为像我们这样想要改变文本位置的人可以通过子类化或简单地添加另一个UILabel(或其他)作为子视图来实现.如果新手开发人员的文本没有出现在标签中或被截断,因为他们没有调整框架,他可能会非常困难.
在我的情况下,我希望文本水平居中,是一个特定的颜色/字体/大小,并在单元格中垂直略高.由于懒得继承这个,我首先尝试改变框架.当这不起作用时,我尝试使用谷歌搜索答案(找到这篇文章).
我的最终解决方案是将numberOfLines属性设置为0并将一些尾随回车符添加到我的文本中.现在这很懒.
| 归档时间: |
|
| 查看次数: |
32802 次 |
| 最近记录: |