2012-10-21 50 views
0

我正在解析URL中的一些數據,並且能夠正確檢索數據。現在我想在自定義tableview中顯示數據,但我無法將數據顯示到表格中。這裏是我的代碼UITableView顯示代碼中的錯誤

- (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];比數據顯示在表中。

+0

顯示聲明[url]的代碼。它是否獲得正確的信息?什麼是[titleNoFormatting]? – Martol1ni

+0

你有沒有試過這個:[cell addSubview:LBtitle]; – kidsid49

+0

titlenotformatting,url和內容是nsmutablearray,我已經從json響應中解析出來,並且我可以將數據導入到所有數組中。如果我將數據顯示爲cell.textlabel.text = [titlenotformatting objectatindex:index path.row]比我能夠顯示數據。所以我不認爲這個問題是在數組或解析 –

回答

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; 
}