0

知道如何移动 UITableViewCell 文本,使其不与左侧的小图像重叠吗?

这是一个屏幕截图:

在此处输入图像描述

谢谢你的帮助

这是我的 TableCell 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UILabel* nameLb;
    UILabel* detailsLb; 

    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:@"log-cell"]; 
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"log-cell"] autorelease];        

        UIImage* img = [ImgUtil image:[NSString stringWithFormat:@"butList3_%d.png", [[SelectCategory instance] select] + 1 ]];
        UIImageView* bgImage = [[[UIImageView alloc] initWithImage:img ]autorelease];
        [cell addSubview:bgImage ];
        [cell sendSubviewToBack:bgImage ];


        nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 250, 40)] autorelease];
        [nameLb setNumberOfLines:4];
        [nameLb setBackgroundColor:[UIColor clearColor]];
        [nameLb setTextColor:[UIColor whiteColor]];
        [nameLb setTag:NAME_TEXT];
        [nameLb setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
        [nameLb setMinimumFontSize:10];

        [cell addSubview:nameLb];

        //add UIImage
        cell.imageView.image = [UIImage imageNamed:@"shop.png"];

        detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 250, 20)] autorelease];
        [detailsLb setBackgroundColor:[UIColor clearColor]];
        [detailsLb setTextColor:[UIColor whiteColor]];
        [detailsLb setTag:DETAILS_TEXT];
        [detailsLb setFont:[UIFont fontWithName:@"Helvetica" size:12]];
        [cell addSubview:detailsLb];


    }else {

        nameLb = (UILabel*)[cell viewWithTag:NAME_TEXT];
        detailsLb = (UILabel*)[cell viewWithTag:DETAILS_TEXT];

    }


    ObjectInfo* obj = [myList objectAtIndex:indexPath.row ] ;
    nameLb.text = [obj name];
//    cell.textLabel.textColor = [UIColor whiteColor];
//    cell.textLabel.font = [UIFont boldSystemFontOfSize:17];

    double distance = [ProjUtil getDistanceWithLat1:currentLocation.latitude long1:currentLocation.longitude lat2:obj.location.latitude long2:obj.location.longitude];
    detailsLb.text = [NSString stringWithFormat:@"%.2f miles", [ProjUtil kmToMi:distance]];
//    cell.detailTextLabel.textColor = [UIColor whiteColor];
//    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setBackgroundColor:[UIColor clearColor]];

    ///[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    return cell;
} 
4

3 回答 3

1

(给出我的一个代码,没有修改 sry)

我看不到图像,但您应该创建一个自定义单元格,您可以在方法中设置单元格控件的框架:-

-(void)layoutSubviews
{

}

示例代码: -

#import <UIKit/UIKit.h>


@interface CustomizedCellProductDetails : UITableViewCell {

    UILabel *sNO;
    UILabel *abcWine;
    UILabel *redWine;
    UILabel *two;
    UILabel *hundred;
    UILabel *fourTwo;
    UILabel *twoOne;
    UIImageView *imgView;

    UILabel *itemNo;
    UILabel *itemName;
    UILabel *itemDesc;
    UILabel *department;
    UILabel *qtyAvailable;

    UIButton *check;

}

@property (nonatomic , retain) UILabel *sNO;
@property (nonatomic , retain) UILabel *abcWine;
@property (nonatomic , retain) UILabel *redWine;
@property (nonatomic , retain) UILabel *two;
@property (nonatomic , retain) UILabel *hundred;
@property (nonatomic , retain) UILabel *fourTwo;
@property (nonatomic , retain) UILabel *twoOne;
@property (nonatomic , retain) UIImageView *imgView;

@property (nonatomic , retain) UILabel *itemNo;
@property (nonatomic , retain) UILabel *itemName;
@property (nonatomic , retain) UILabel *itemDesc;
@property (nonatomic , retain) UILabel *department;
@property (nonatomic , retain) UILabel *qtyAvailable;
@property (nonatomic , retain) UIButton *check;

-(void) clicked;
@end


#import "CustomizedCellProductDetails.h"


@implementation CustomizedCellProductDetails
@synthesize sNO,abcWine,redWine,two,hundred,fourTwo,twoOne,imgView,itemNo,itemName,itemDesc,department,qtyAvailable,check;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        sNO=[[UILabel alloc] init];
        abcWine=[[UILabel alloc] init];
        redWine=[[UILabel alloc] init];
        two=[[UILabel alloc] init];
        hundred=[[UILabel alloc] init];
        fourTwo=[[UILabel alloc] init];
        twoOne=[[UILabel alloc] init];
        imgView=[[UIImageView alloc] init];

        itemNo=[[UILabel alloc] init];
        itemName=[[UILabel alloc] init];
        itemDesc=[[UILabel alloc] init];
        department=[[UILabel alloc]init];
        qtyAvailable=[[UILabel alloc] init];

        check=[UIButton buttonWithType:UIButtonTypeCustom];
        [check addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
        [check setTitle:@"Check" forState:UIControlStateNormal];

        [self.contentView addSubview:sNO];
        [self.contentView addSubview:abcWine];
        [self.contentView addSubview:redWine];
        [self.contentView addSubview:two];
        [self.contentView addSubview:hundred];
        [self.contentView addSubview:fourTwo];
        [self.contentView addSubview:twoOne];
        [self.contentView addSubview:itemNo];
        [self.contentView addSubview:itemName];
        [self.contentView addSubview:itemDesc];
        [self.contentView addSubview:department];
        [self.contentView addSubview:qtyAvailable];
        [self.contentView addSubview:check];

    }
    return self;
}

- (void)layoutSubviews {

    [super layoutSubviews];

    CGRect contentRect = self.contentView.bounds;

    CGFloat boundsX = contentRect.origin.x;

    CGRect frame;

    frame=CGRectMake(boundsX+10, 0, 50, 40);

    sNO.frame = frame;

    frame= CGRectMake(boundsX+70 ,0, 150, 40);

    abcWine.frame = frame;

    frame= CGRectMake(boundsX+230 ,0, 150, 40);

    redWine.frame = frame;

    frame= CGRectMake(boundsX+390 ,0, 50, 40);

    two.frame = frame;

    frame= CGRectMake(boundsX+450 ,0, 50, 40);

    hundred.frame = frame;

    frame= CGRectMake(boundsX+510 ,0, 50, 40);

    fourTwo.frame = frame;

    frame= CGRectMake(boundsX+570 ,0, 50, 40);

    twoOne.frame = frame;

    /************************                      ***********************/

    frame= CGRectMake(boundsX+10 ,50, 100, 200);

    imgView.frame = frame;

    }

-(void) clicked
{

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state.
}


- (void)dealloc {
    [super dealloc];
}


@end

还可以更改 uitableviewcell 的 cellForRowAtIndexPath 委托方法中的高度。

于 2012-05-15T11:09:12.470 回答
1

在 cellForRowAtIndexPath 中生成单元格时,使用此代码更新标签的创建以正确设置框架

nameLb = [[[UILabel alloc] initWithFrame:CGRectMake(bgImage.frame.origin.x + bgImage.frame.size.width + 5, 5, 250, 40)] autorelease];
detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(bgImage.frame.origin.x + bgImage.frame.size.width + 5, 5, 250, 40)] autorelease];

这将在图像右侧动态创建您的自定义行文本。

于 2012-05-15T11:09:36.710 回答
1

创建标签时,您将使用以下行定位它们:

detailsLb = [[[UILabel alloc] initWithFrame:CGRectMake(5, 45, 250, 20)] autorelease];

这会设置标签的框架。CGRectMake 是一个为您提供 CGRect 的函数,在本例中为原点 (5,45),宽度为 250,高度为 20。

如果要将标签向右移动,请增加框架的 x 值(上面的 CGRectMake 调用中的 5)。

于 2012-05-15T11:19:28.080 回答