如何在iPhone中的单元格上显示xml值

时间:2011-06-30 07:20:24

标签: iphone objective-c

我从xml中获取值。我正在提取它的工作正常,但我面临着在单元格上显示值的问题。我必须在第一个单元格和第二个单元格4值上显示6个值,我该怎么做?因为在我的第一个单元格中,该值在下一个单元格中重复相同。

这是我的手机代码:

#import <UIKit/UIKit.h>
#import "TWeatherParser.h"
@class TWeatherParser;


@interface TWeatherController : UITableViewController {

    UITableView *mTableView;
    NSMutableArray *mImage;
    NSMutableArray *weatherarray;
    TWeatherParser *weather;
}
@property (nonatomic, retain) IBOutlet UITableView *mTableView;
@property (nonatomic, retain) NSMutableArray *weatherarray;
@property (nonatomic, retain) TWeatherParser *weather;

@end





    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";


       TWeatherCell *cell =(TWeatherCell *) [mTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[TWeatherCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier] autorelease];
        }
        TWeatherElement *newobj = [weatherarray objectAtIndex:indexPath.row];
        if ([newobj.icon isEqualToString:@"http://\n"])
        {
            cell.weatherimage.image = [UIImage imageNamed:@"listIcon-H.png"];
        }
        else {
            NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:newobj.icon]];
            cell.weatherimage.image = [UIImage imageWithData:imageData];
            [imageData release];
        }
        cell.reportdate.text = newobj.currentdate;
        NSLog(@"this is cell1 value:%@",cell.reportdate.text);
        cell.conditionname.text = newobj.conditionname;
        NSLog(@"this is cell2 value:%@",cell.conditionname.text);
        cell.twotemp.text = [NSString stringWithFormat:@"Temp:%@/%@",newobj.mintemp,newobj.maxtemp];
        NSLog(@"this is cell3 value:%@",cell.twotemp.text);
        cell.twodirection.text = newobj.wind;
        NSLog(@"this is cell4 value:%@",cell.twodirection.text);
        cell.humidity.text = newobj.humidity;
        NSLog(@"this is cell5 value:%@",cell.humidity.text);
        //cell.reportdate.text = newobj.currentdate;


        //cell.reportdate.text =@"My journey";
    //  cell.conditionname.text = @"raji";
    //  cell.twotemp.text = @"pradeep"; 
    //  cell.twodirection.text = @"harish";
    //  cell.humidity.text =@"23";
    //  cell.weatherimage.image = [UIImage imageNamed:@"listIcon-H.png"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        // Configure the cell...

        return cell;
    }

3 个答案:

答案 0 :(得分:1)

如果要在不同的单元格中显示不同的值,则必须使用switch case来检测行索引:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
  static NSString *CellIdentifier = @"Cell";

ListDetailCell *cell= [[[ListDetailCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

tableView.backgroundColor = [UIColor whiteColor];

if (cell == nil) 
{
    cell = [[[ListDetailCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
            autorelease];
}

switch (indexPath.row)
{
    case 0:
        NSLog(@"%d",indexPath.row);
        cell.leadingLabel.text = @"Name: ";
        cell.leadingLabel.font = [UIFont fontWithName:LABELS_FONT_NAME_BOLD size:11.0f];
        cell.leadingLabel.textColor = FONT_GREEN_COLOR;

        cell.volInfo.text = volRow.volName;
        cell.volInfo.font = [UIFont fontWithName:LABELS_FONT_NAME size:11.0f];
        break;
    case 1:
        NSLog(@"%d",indexPath.row);
        cell.leadingLabel.text = @"Address: ";
        cell.leadingLabel.font = [UIFont fontWithName:LABELS_FONT_NAME_BOLD size:11.0f];
        cell.leadingLabel.textColor = FONT_GREEN_COLOR;

        cell.volInfo.text = volRow.volAddress;
        cell.volInfo.font = [UIFont fontWithName:LABELS_FONT_NAME size:11.0f];
        break;
    case 2:
        NSLog(@"%d",indexPath.row);
        cell.leadingLabel.text = @"Phone: ";
        cell.leadingLabel.font = [UIFont fontWithName:LABELS_FONT_NAME_BOLD size:11.0f];
        cell.leadingLabel.textColor = FONT_GREEN_COLOR;

        cell.volInfo.text = volRow.phone;
        cell.volInfo.font = [UIFont fontWithName:LABELS_FONT_NAME size:11.0f];
        break;
    case 3:
        NSLog(@"%d",indexPath.row);
        cell.leadingLabel.text = @"Email: ";
        cell.leadingLabel.font = [UIFont fontWithName:LABELS_FONT_NAME_BOLD size:11.0f];
        cell.leadingLabel.textColor = FONT_GREEN_COLOR;

        cell.volInfo.text = volRow.email;
        cell.volInfo.font = [UIFont fontWithName:LABELS_FONT_NAME size:11.0f];
        break;

    default:
        NSLog(@"Out of Range ",indexPath.row);
        break;
}

return cell;
}

答案 1 :(得分:0)

使用NSXMLParser并解析xml文件,然后按键获取值并制作并制作字典并存储在数组中。

然后cellForRowAtIndexPath中的代码方式足以显示数据。但要相应地做出改变......

答案 2 :(得分:0)

您必须使用如下所示的唯一标识符字符串标识每个单元格 -

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell_%d", indexPath.row]];

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:@"cell_%d", indexPath.row]] autorelease];
}

通过这样做,您将使tableView能够重复使用相同的单元格(第一次),然后再使用6个项目或4个项目。