如何自定义UITableViewCell以使文本不重叠

时间:2012-05-15 11:06:10

标签: iphone objective-c ios uitableview

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

这是截图:

enter image description here

感谢您的帮助

这是我的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;
} 

3 个答案:

答案 0 :(得分:1)

(Givin我的一个代码,widout修改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委托方法的高度。

答案 1 :(得分: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];

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

答案 2 :(得分:1)

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

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

这会设置标签的框架。 CGRectMake是一个函数,它给你一个CGRect,在这种情况下有原点(5,45),宽度250和高度20.

如果要将标签移到右侧,请增加帧的x值(上面CGRectMake调用中的5)。