开发者

How to display xml value on cell in iPhone

I'm fetching the value from xml. What I'm fetching it's working good but I face the problem in displaying the value on a cell. I have to display 6 values on first cell and second cell 4 values how can i do this? Because in my first cell, the value is repeated same on next cell.

This is my cell code:

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


If you want to show different values in different cell then you have to use switch case to detect row index :

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


Use NSXMLParser and parse the xml file then fetch values by keys and make and make a dictionary and store in your array.

Then your way of code in cellForRowAtIndexPath is good enough to show data. But make change accordingly...


You will have to identify each cell with unique identifier string like below -

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

By doing this, you will make tableView able to reuse the same cell created (first time) before either with 6 items or 4 items.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜