To use XML element values as text in a table view cell

Asked 2 years ago, Updated 2 years ago, 116 views

Creating an iPhone app

How do I use XML element values as text in a table view cell?

Specifically, it's as follows

  • Retrieve XML element values that are the contents of _iremono (NSString type) created with method 1
  • I want to use the acquired element value in Method 2 as the text of the table view
 // 要素 When element values are found
- (void) parser: (NSXMLParser*) parser
foundCharacters: (NSString*) string {

   // Determining Element Names
   if([_nowElemisEqualToString:@"item"){

     // The type of instance variable '_nowElem' as the element name (most recent) being analyzed is
     // "NSMutableString".
     NSLog(@"Element value: %@", string);

     // The type of instance variable "_iremono" as an inclusion of the argument "string" is:
     // "NSString*".
     _iremono=string;

   }
}

// 設定 Settings (cells)
- (UITableViewCell*) tableView: (UITableView*) tableView
         cellForRowAtIndexPath:(NSIndexPath*)indexPath{

   // Cell Object Settings
   UITableViewCell* cell=
   [tableView requestReusableCellWithIdentifier: @"Cell"
                  forIndexPath:indexPath];


   // Settings (cell) *Instead of this @"InherentVice" I would like to include the element value obtained in で.
   cell.textLabel.text=@"Inherent Vice";
   cell.textLabel.textColor=[UIColor blackColor];


   return cell;
}

Could someone please let me know the above?Thank you for your cooperation.

objective-c ios8 tableview xml

2022-09-29 21:39

2 Answers

If you are able to set _iremono to a parse of xml,
@ Wouldn't it be all right to change "Inherent Vice" to _iremono?

cell.textLabel.text=_iremono;

If you have more than one element on the cell and you have not been able to retrieve the target label,
I think you can get it using the following method.

// Cell object settings
UITableViewCell* cell=
[tableView requestReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

UILabel*label=(UILabel*) [cell viewWithTag:<tag> on target labels;];
label.text=_iremono;


2022-09-29 21:39

Before considering the relationship between XML and NSXMLParser, consideration of UITableView itself should be deepened.
UITableView does not have a mechanism to hold its own data. For example, UIImageView has a property image that holds its own image data, so UITableView also has a property data that can hold its own data.But that is not the case.
"MVC design pattern" is the concept.It's a program development guideline that's as popular as Wikipedia, but to put it simply, it's a guideline that data (Model) and view should be separated into individual objects.iOS is configured to comply with this MVC.The object that mediates data and display objects is called a controller, and that is why many classes are found during UIKit with a name of ConControllerer.
UITableView does not have data internally as a result of compliance with this MVC.If so, following MVC will lead to reasonable app design.
The view object should be a UITableView and the controller should be a UITableViewController or a UITableViewController.That's what happens all of a sudden.You can apply a variety of model objects.For very large and complex data, CoreData can be used to model relational databases such as SQLite, or it can be designed to create classes for data retention and keep instances resident.If it's simple, it's easier to model an object in NSMutableArray.
Here, as an instance variable of ViewController, we create an instance of NSMutableArray called "dataArray" and use it as a model object.

-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string{
   if([_nowElemisEqualToString:@"item"){
          dataArray addObject:string;
   }
}

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath{
   UITableViewCell* cell=
   [tableView requestReusableCellWithIdentifier: @"Cell"
                  forIndexPath:indexPath];
   cell.textLabel.text=dataArray [indexPath.row];
   cell.textLabel.textColor=[UIColor blackColor];

   return cell;
}

Note:
The sample code uses a new Objective-C 2.0 format and a simplified NSArray format.

dataArray [indexPath.row];

Non-simplified format:

[dataArray objectAtIndex:indexPath.row];


2022-09-29 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.