UITableView显示代码中的错误
问题描述:
我正在解析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];
比数据显示在表中。
答
- (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;
}
显示声明[url]的代码。它是否获得正确的信息?什么是[titleNoFormatting]? – Martol1ni
你有没有试过这个:[cell addSubview:LBtitle]; – kidsid49
titlenotformatting,url和内容是nsmutablearray,我已经从json响应中解析出来,并且我可以将数据导入到所有数组中。如果我将数据显示为cell.textlabel.text = [titlenotformatting objectatindex:index path.row]比我能够显示数据。所以我不认为这个问题是在数组或解析 –