我想解析 xml 并在表格单元格中显示标题、描述和图像。
我做了以下代码,但如果图像不可用,那么单元格上也会显示垃圾图像。
我做了代码来隐藏它,但图像视图没有隐藏。
此外,我将 frame 设置为零值,但显示带有垃圾图像的图像视图:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
appDelegate = (AppDelegate_iPhone *) [[UIApplication sharedApplication] delegate];
if (section == 0) {
return [appDelegate.TrendDataArray count];
}else {
return [appDelegate.MyDataArray count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
return @" Trending ";
else
return @" Live Feeds ";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *lblTitle;
UILabel *lbldescription;
UIImageView *imgViewTemp;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
lblTitle = [[UILabel alloc] initWithFrame:CGRectZero];
[lblTitle setLineBreakMode:UILineBreakModeWordWrap];
[lblTitle setNumberOfLines:2];
lblTitle.textAlignment= UITextAlignmentLeft;
[lblTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT]];
lblTitle.textColor=[UIColor blackColor];
[lblTitle setTag:1];
lbldescription = [[UILabel alloc] initWithFrame:CGRectZero];
[lbldescription setLineBreakMode:UILineBreakModeWordWrap];
[lbldescription setNumberOfLines:3];
lbldescription.textAlignment= UITextAlignmentLeft;
[lbldescription setFont:[UIFont fontWithName:@"Helvetica" size:FONT2]];
lbldescription.textColor=[UIColor grayColor];
[lbldescription sizeToFit];
[lbldescription setTag:2];
imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectZero];
[imgViewTemp setTag:3];
[cell.contentView addSubview:lblTitle];
[cell.contentView addSubview:lbldescription];
//[imgViewTemp release];
[lblTitle release];
[lbldescription release];
}
// Configure the cell...
if (indexPath.section == 0) {
appDelegate=(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
objXmlParse_Attributes= [appDelegate.TrendDataArray objectAtIndex:indexPath.row];
}else if (indexPath.section == 1) {
appDelegate=(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
objXmlParse_Attributes= [appDelegate.MyDataArray objectAtIndex:indexPath.row];
}
NSString *text = objXmlParse_Attributes.CommenUrlTitle;
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *text2 = objXmlParse_Attributes.CommenUrlDescription;
text2 = [text2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
CGSize constraint = CGSizeMake(WIDTH - (MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGSize size2 = [text2 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:FONT2] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
NSString *link = [NSString stringWithFormat:@"%@", objXmlParse_Attributes.CommenUrlImage];
//imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectZero];
//[UIImage imageNamed:@"noimage.png"]
if([link isEqualToString:@"No image"] || [link isEqualToString:@"no image"]){
link = @"";
if ([link isEqualToString:@""]) {
imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,0,0)];
//[imgViewTemp release];
lblTitle = (UILabel*)[cell.contentView viewWithTag:1];
[lblTitle setText:text];
[lblTitle setFrame:CGRectMake(MARGIN+3,MARGIN, WIDTH-110+83,MIN(size.height,30))];
lbldescription = (UILabel*)[cell.contentView viewWithTag:2];
[lbldescription setText:text2];
[lbldescription setFrame:CGRectMake(MARGIN+3,MARGIN + MIN(size.height,30),WIDTH-110+83,MIN(size2.height,40))];
}
}
else{
//imgViewTemp = (UIImageView*)[cell.contentView viewWithTag:3];
imgViewTemp = [[UIImageView alloc] initWithFrame:CGRectMake(7,11,73,60)];
[imgViewTemp setImageWithURL:[NSURL URLWithString:link] placeholderImage:nil];
//image1.hidden=FALSE;
lblTitle = (UILabel*)[cell.contentView viewWithTag:1];
[lblTitle setText:text];
[lblTitle setFrame:CGRectMake(MARGIN + 83, MARGIN, WIDTH-110,MIN(size.height,30))];
lbldescription = (UILabel*)[cell.contentView viewWithTag:2];
[lbldescription setText:text2];
[lbldescription setFrame:CGRectMake(MARGIN + 83, MARGIN + MIN(size.height,30),WIDTH-110,MIN(size2.height,40))];
[cell.contentView addSubview:imgViewTemp];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}