-1

我上传了图像,它通过使用此代码向您展示它如何在 uitablevirw 中显示内容

- (void)viewDidLoad
    {
        NSURL *url=[[NSURL alloc]initWithString:@"http://avweatherforecasts.com/services/iphoneservice.php"];    
        DaTa=[[NSData alloc]initWithContentsOfURL:url];    
        NSLog(@"data----%@",DaTa);
        Xmlpaeser=[[NSXMLParser alloc]initWithData:DaTa];    
        Xmlpaeser.delegate=self;    
        [Xmlpaeser parse];
        [Xmlpaeser release];

        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }



 - (void)parserDidStartDocument:(NSXMLParser *)parser{
        array=[[NSMutableArray alloc]init];
        strcontent=nil;
    }

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{   

        if ([elementName isEqualToString:@"start"])
        {        
            Dict=[[NSMutableDictionary alloc]init];    
      }
    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

        strcontent=[NSString stringWithFormat:@"%@",string];

        NSLog(@"Strcontent----%@",strcontent);
    }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{


            if ([elementName isEqualToString:@"city"]){
                [Dict setObject:strcontent forKey:elementName];
            }
            if ([elementName isEqualToString:@"min"]){
                [Dict setObject:strcontent forKey:elementName];
            }
            if ([elementName isEqualToString:@"max"]){
                [Dict setObject:strcontent forKey:elementName];
            }
            if ( [elementName isEqualToString:@"forecast"]
                ) {

                [Dict setObject:strcontent forKey:elementName];
               NSLog(@"dict--%@",[Dict description]);
            }          
            else if([elementName isEqualToString:@"start"]){            
                [array addObject:Dict];

                [Dict release];     
        }
    }

    - (void)parserDidEndDocument:(NSXMLParser *)parser{

        NSLog(@"array--%@",[array description]);       

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return [array count];

    }

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

        static NSString *identifier=@"cell";

        custome_cell *Cell=(custome_cell *)[tableView dequeueReusableCellWithIdentifier:identifier];

        if (Cell==nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"custome_cell" owner:self options:nil];
            Cell = [nib objectAtIndex:indexPath.row];
        }    
        Cell.city.text=[[array objectAtIndex:indexPath.row] objectForKey:@"city"];
        Cell.min.text=[[array objectAtIndex:indexPath.row] objectForKey:@"min"];
        Cell.max.text=[[array objectAtIndex:indexPath.row] objectForKey:@"max"];
        Cell.forecast.text =[[array objectAtIndex:indexPath.row] objectForKey:@"forecast"];

        return Cell;    
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 80;
    }
4

1 回答 1

2

试试这个......也许它会帮助你......

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      return [CommentsGetArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
        //[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;



        commentsTextView = [[UITextView alloc]initWithFrame:CGRectMake(90, 4, 190, 50)];
        commentsTextView.delegate = self;
        commentsTextView.tag = 101;
        commentsTextView.textAlignment = UITextAlignmentLeft;

        [commentsTextView setScrollEnabled:YES];

        commentsTextView.backgroundColor =[UIColor clearColor];
        commentsTextView.font = [UIFont fontWithName:@"Arial" size:12.0];
        commentsTextView.textColor =[UIColor blackColor];
        commentsTextView.editable = NO;
        [cell addSubview:commentsTextView];



    }



     commentsTextView = (UITextView *)[cell viewWithTag:101];
    commentsTextView.text = [[CommentsGetArray objectAtIndex:indexPath.row]objectForKey:@"comment"];


    return cell;

}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    return 70;
}

您可以将其用作标签而不是 uitextView,并且您可以创建更多文本视图或标签来显示您的数据,但请记住更改它们的框架。

于 2013-01-22T07:45:43.023 回答