首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableView中的对中图像

UITableView中的对中图像
EN

Stack Overflow用户
提问于 2014-01-08 16:35:13
回答 6查看 173关注 0票数 1

我在UITableViewCell中的图像中心有很多问题。我花了无数个小时来尝试堆栈溢出上列出的所有解决方案,以及无数的其他网站。我的文本将居中,但出于某种原因,我的图像不会居中,表格的左边有一条白线,我也无法去掉。(据我在iOS文档中所读到的,这在iOS 7中是新的,但人们已经让它消失了),我已经通过检查器将所有的嵌入设置为零。我正在使用核心数据来检索图像,这是很好的工作,这只是我的图像不会中心。

下面是我对tableViewCell的方法

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:CellIdentifier];
}

// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]];
UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:@"image"]];
cell.imageView.image = cellImage;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor whiteColor];



return cell;
}

此外,我似乎无法让我的标签超出图像,这是另外一个问题。但是,为了修复这个问题,我所做的就是做一个定制的单元格子类,但是这个子类没有工作。我没有足够的资源和想法来解决这个问题。

编辑自定义子类,因此我可能对自定义UITableViewCell子类有错误的想法。但是我所做的是在一个名为UIImageView的类中创建一个类,其中有两个变量,一个用于UIImageView,另一个用于标签。然后,在主表视图中,我这样称呼它们:

代码语言:javascript
复制
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:CellIdentifier];
}

// Configure the cell...
NSManagedObject *moment = [self.moments objectAtIndex:indexPath.row];
//cell.textLabel.text = [NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]];
//UIImage *cellImage = [UIImage imageWithData:[moment valueForKey:@"image"]];
//cell.imageView.image = cellImage;
//cell.textLabel.textAlignment = NSTextAlignmentCenter;
//cell.textLabel.textColor = [UIColor whiteColor];
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[customCell.cellTitle setText:[NSString stringWithFormat:@"%@", [moment valueForKey:@"name"]]];
[customCell.cellImage setImage:[UIImage imageWithData:[moment valueForKey:@"image"]]];



return cell;
}

然后,简单地在自定义单元格类中,我将标签和图像连接到一个IBOutlet,并将它们合成。

编辑2自定义子类加法

头文件有两个属性。一个用于cellTitle,它是一个UILabel,一个用于cellImage,它是一个UIImageView。

代码语言:javascript
复制
@interface CustomCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *cellImage;
@property (weak, nonatomic) IBOutlet UILabel *cellTitle;

实施文件:

这里我只是综合这两个物体。

代码语言:javascript
复制
@synthesize cellImage;
@synthesize cellTitle; 

- (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
}
EN

Stack Overflow用户

发布于 2014-01-08 19:07:18

对单元格子类创建一个方法

代码语言:javascript
复制
- (void)layoutSubviews {
    self.cellTitle.frame = CGRectMake(...); //self.view.frame will return the cell's frame
    self.cellImage.frame = CGRectMake(...); //you should set the frames of title and imageView however you want them in here
}
票数 0
EN
查看全部 6 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21001343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档