1
我想解析xml並在表格單元格中顯示標題,描述和圖像。如何在兩個不同部分的兩個不同的NSMutableArray中顯示單元格中的內容(兩個標籤和一個圖像視圖)。
我做了下面的代碼,但如果圖像不可用,那麼垃圾圖像也顯示在單元格上。
我做了隱藏它的代碼,但圖像視圖沒有隱藏。
我設立幀零個值,但與垃圾的圖片視圖顯示:
- (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;
}
我解決使用該溶液 如果(部分== 0){ // 這裏我的用於從ARRAY1 添加標籤和圖像代碼}如果(部分== 1){// 這裏我的用於添加的標籤的代碼和array2的圖片 } –
ok恭喜... – Rajneesh071