0

我正在从 URL 解析一些数据,并且能够正确检索数据。现在我想在自定义表格视图中显示该数据,但我无法将数据显示到表格中。这是我的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [url count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

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

UILabel *LBtitle = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250, 40)];
LBtitle.font = [UIFont boldSystemFontOfSize:13.0];
LBtitle.numberOfLines=2;
[LBtitle sizeToFit];

UILabel *LBurl = [[UILabel alloc]initWithFrame:CGRectMake(60, 45, 250, 20)];
LBurl.font = [UIFont systemFontOfSize:13.0];
LBurl.textColor = [UIColor blueColor];
LBurl.numberOfLines=1;
[LBurl sizeToFit];

UILabel *LBcontent = [[UILabel alloc]initWithFrame:CGRectMake(60, 65, 250, 60)];
LBcontent.font = [UIFont systemFontOfSize:13.0];
LBcontent.textColor = [UIColor grayColor];
LBcontent.numberOfLines=3;
[LBcontent sizeToFit];

LBtitle.text = [titleNoFormatting objectAtIndex:indexPath.row];
LBurl.text = [url objectAtIndex:indexPath.row];
LBcontent.text = [content objectAtIndex:indexPath.row];

[cell.contentView addSubview:LBtitle];
[cell.contentView addSubview:LBurl];
[cell.contentView addSubview:LBcontent];


cell.selectionStyle = UITableViewCellSelectionStyleNone;

//cell.textLabel.text = [titleNoFormatting objectAtIndex:indexPath.row];
//cell.detailTextLabel.text = [url objectAtIndex:indexPath.row];
// Configure the cell...


if (indexPath.row % 2 == 0) {
    cell.backgroundColor = [UIColor whiteColor];
}
else{
    cell.backgroundColor = [UIColor colorWithRed:231/255.0 green:227/255.0 blue:227/255.0 alpha:1.0];
}

return cell;
}

如果我写cell.textlabel.text = [titleNoFormatting objectAtIndex:indexPath.row];比数据显示在表中。

4

1 回答 1

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


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

        **//create label should put in this block**

        UILabel *LBtitle = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250, 40)];
        LBtitle.font = [UIFont boldSystemFontOfSize:13.0];
        LBtitle.numberOfLines=2;
        LBtitle.tag = 1;


        UILabel *LBurl = [[UILabel alloc]initWithFrame:CGRectMake(60, 45, 250, 20)];
        LBurl.font = [UIFont systemFontOfSize:13.0];
        LBurl.textColor = [UIColor blueColor];
        LBurl.numberOfLines=1;
        LBurl.tag = 2;


        UILabel *LBcontent = [[UILabel alloc]initWithFrame:CGRectMake(60, 65, 250, 60)];
        LBcontent.font = [UIFont systemFontOfSize:13.0];
        LBcontent.textColor = [UIColor grayColor];
        LBcontent.numberOfLines=3;
        LBcontent.tag = 3;


        [cell.contentView addSubview:LBtitle];
        [cell.contentView addSubview:LBurl];
        [cell.contentView addSubview:LBcontent];


    }

    UILabel *LBtitle = (UILabel*)[cell.contentView viewWithTag:1];
    UILabel *LBurl = (UILabel*)[cell.contentView viewWithTag:2];
    UILabel *LBcontent = (UILabel*)[cell.contentView viewWithTag:3];

    LBtitle.text = @"test";
    LBurl.text = @"test";
    LBcontent.text = @"agassi";

    **sizeToFit should put here**
    [LBtitle sizeToFit];
    [LBurl sizeToFit];
    [LBcontent sizeToFit];


    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    //cell.textLabel.text = [titleNoFormatting objectAtIndex:indexPath.row];
    //cell.detailTextLabel.text = [url objectAtIndex:indexPath.row];
    // Configure the cell...


    if (indexPath.row % 2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }
    else{
        cell.backgroundColor = [UIColor colorWithRed:231/255.0 green:227/255.0 blue:227/255.0 alpha:1.0];
    }

    return cell;
}
于 2012-10-21T12:53:00.227 回答