UITableView显示代码中的错误

时间:2012-10-21 12:06:39

标签: ios

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

- (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];而不是数据显示在表格中。

1 个答案:

答案 0 :(得分: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;
}