开发者

how to have multiple labels in table cell without making custom table view iphone

i have a not custom table view view that i mean that i'm using the cellForRowAtIndexPath. I want my cell to have 2 labels one for a title and the other for a date. I have tried to use this code that doesn't work:

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f); 

    UIView* view = cell.contentView;
    UILabel* labelTitle = [[UILabel alloc]开发者_运维技巧 initWithFrame:myImageRect];
    UILabel* labelDate = [[UILabel alloc] initWithFrame:myImageRect];

    [cell addSubview:labelTitle];
    [cell addSubview:labelDate];

    cell.labelDate.text = @"title";
    cell.labelTitle.text = @"date";

I get an error saying: Property 'labelDate' not found on object of type 'UITableViewCell *'

So it seems that the cell doesn't recognize my labels. Can thsi be done in some way?

Thanks in advance.


You have to use table view delegate methods as;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *detailField_Name = [[NSString alloc] init];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = @"title";
cell.detailTextLabel.text = @"date";

return cell;

}


try

 [cell.contentView addSubview:title];


Here is a good example from Apple how to make some custom cells.


use

[cell.contentView addSubview:title];

instead of

[cell addSubview:labelTitle];

and

labelDate.text = @"title";

instead of

cell.labelDate.text = @"title";


You can add multiple label in below way:

UILabel *lblTaskTitle = [[UILabel alloc] initWithFrame:CGRectMake(45.0, 5, 100.0, 35.0)];

    [lblTaskTitle setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];        
    [lblTaskTitle setTextColor:[UIColor whiteColor]];
    [lblTaskTitle setBackgroundColor:[UIColor clearColor]];
    lblTaskTitle.text = oTaskType.taskTypeTitle;
    [cell addSubview:lblTaskTitle];

UILabel *lblTaskDetail = [[UILabel alloc] initWithFrame:CGRectMake(45.0, 5, 140.0, 35.0)];

    [lblTaskDetail setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];       
    [lblTaskDetail setTextColor:[UIColor whiteColor]];
    [lblTaskDetail setBackgroundColor:[UIColor clearColor]];
    lblTaskDetail.text = oTaskType.taskTypeTitle;
    [cell addSubview:lblTaskDetail];

Let me know in case of any difficulty.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜