How to populate UITableView with plist - uitableview

I have a simple UITableView that I'd like to populate using a plist file. I have followed a few tutorials but they all seem to over complicate the process and in the end I end up getting errors or my app simply does not work.
I'm using a window based application.
Cheers,
Sam

To load the file :
thearray = [NSArray arrayWithContentsOfFile:thePath];
Then you need to set your controller as the data source and delegate for the table view and implement at least :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [thearray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
// assuming your array contains only simple strings :
cell.textLabel.text = [thearray objectAtIndex:indexPath.row];
return cell;
}
This doesn't use the "reuse" of cells which is important if your list is big.

Related

UITableView does not load data

I am relative new to iOS development. Right now, I created 2 ViewController using storyboard. The one consist one button that lead into another controller using segue (show).
This controller is TableViewController that embedded in Navigation Controller and already connected with its responsible class that inherit from UITableViewController.
My problem is this page doesn't load its data (NSMutableArray) that already initialized in viewDidLoad using
_dataClient = [NSMutableArray arrayWithObjects:#"First City",#"Second City",#"Third City",#"Forth City",#"Fift City", nil];
This is my table delegate and datasource:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_dataClient count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"cellItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_dataClient objectAtIndex:indexPath.row];
return cell;
}
Is there any step that I forgot to do?
You must return 1 section at least. Change this line:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; /// here
}

How to access objects inside an array object

I am creating an application for IOS and am struggling with accessing an object inside an array i have made for display on the table view cell.
This is the code i have used to add the object to the array every time the loop cycles through.
for (int i = 0; i < [parsedArray count]; i++) {
HPTBusStops *busStops = [[HPTBusStops alloc] init];
NSArray *informationArray = [parsedArray objectAtIndex:i];
busStops.busStopNumber = [informationArray objectAtIndex:0];
busStops.busStopName = [informationArray objectAtIndex:1];
busStops.latitude = [informationArray objectAtIndex:2];
busStops.longitude = [informationArray objectAtIndex:3];
[self.busStopsHolder addObject:busStops];
}
The HPTBusStops class is obviously custom, and later in the master view controller, i am tring to re-access these properties through the busStopsHolder array, when programming the cell, in this part:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HPTBusStopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BusStopCell" forIndexPath:indexPath];
return cell;
}
I am honestly very unsure of how to access the busStops object's properties through the busStopHolder's array.
Any help would be appreciated
Thanks
Hamish
It looks to me like you might have an issue with scope...
You'll need to make "busStopsHolder" a #property of the class containing the for loop and instantiate that class as an instance variable in your Master View Controller.
in your class, say MyInfoClass.h:
#property (strong, nonatomic) NSMutableArray *busStopsHolder;
in MyInfoClass.m:
synthesize busStopsHolder;
Make sure you have initialized your busStopsHolder array in MyInfoClass...
(id) init {
busStopsHolder = [[NSMutableArray alloc] init];
}
Then in your master view controller .h:
#import "MyInfoClass.h"
#interface myMastserViewController : UIViewController {
MyInfoClass *infoClass;
}
Then...
- (void) viewDidLoad {
infoClass = [[MyInfoClass alloc] init];
[infoClass methodToLoadBusStops];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HPTBusStopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BusStopCell" forIndexPath:indexPath];
HPTBusStop *busStop = [[infoClass busStopsHolder] objectAtIndex:indexPath.row];
[[cell textLabel] setText:[busStop busStopName]]
return cell;
}
An oversimplified way to do this is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HPTBusStopsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BusStopCell" forIndexPath:indexPath];
HPTBusStops *busStops = yourSourceArray[indexPath.row];
cell.textLabel.text = busStops.name;
return cell;
}
If your tableView has one section, you can use the tableView's indexPath.row property to determine which index you're dealing with, use that as the index to access the relevant object in your source array, and then customize the cell based on the specific object at that index. In the example code I just assumed there was a name property on the busStops object for example purposes.
Make sure in your numberOfRowsInSection method you set the number of rows as the number of objects in your source array, whatever that is.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return yourSourceArray.count;
}

UITableView Cell ImageView issue [duplicate]

This question already has answers here:
Is there a way to make UITableView cells in iOS 7 not have a line break in the separator?
(17 answers)
Closed 8 years ago.
Hi
I have a UITableView in my class and when i tried to load the contents am getting a screen like this
Below the white box am not able to see the horizodal separator line , Please help me.
My code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [names count]; //count number of row from counting array hear cataGorry is An Array
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"service";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier]
;
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
cell.imageView.frame=CGRectMake(5,10,50,60);
cell.imageView.image=[imaages objectAtIndex:indexPath.row];
cell.textLabel.text = [names objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor clearColor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
write in your view did load
In iOS 7 and later, cell separators do not extend all the way to the edge of the table view.This property sets the default inset for all cells in the table, much like rowHeight sets the default height for cells. It is also used for managing the “extra” separators drawn at the bottom of plain style tables.
// Do any additional setup after loading the view.
if ([_tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[_tableView setSeparatorInset:UIEdgeInsetsZero];
}

UITableview grouped style only showing one value from array?

MY UITableview Group style only showing one value from the array of 4 elements.
in viewdidload i add following array
_displayThemeItems=[[NSArray alloc]initWithObjects:#"Default",#"Night",#"Sepia" ,#"Blue" ,nil];
_displayThemeIcons=[[NSArray alloc]initWithObjects:[UIImage imageNamed:#"day.png"],[UIImage imageNamed:#"night.png"],[UIImage imageNamed:#"sepia.png"], [UIImage imageNamed:#"blue.png"],nil];
and my table delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_displayThemeItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// No cell available - create one.
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];}
NSLog(#"rray%#",_displayThemeItems);
cell.textLabel.text = [_displayThemeItems objectAtIndex:indexPath.row];
cell.imageView.image=[_displayThemeIcons objectAtIndex:indexPath.row];
return cell;
}
Your code look fine to me, the only the only reason that I can think amy effect the result you see is how you set up your table view.
Make sure that your table view frame is bug enough to show the 4 items, and make sure you set the tabe view data source.
Add the "}" just before the return such that the code where you set the title and image comes under the "if" statement.

Advice on creating table with dynamic data in IOS

I am trying to create a table with dynamic data and I'm kind of stuck. Here is how my data is structured :
NSMutableArray *bigArray;
bigArray has many NSDictionary items.
each items has only one entry.
sectionName is the key, NSMutableArray is the value.
There are many objects in the value NSMutableArray.
I tried to explain this as simple as I could, here is the part where I'm stuck.
//easy
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [bigArray count];
}
//medium
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[[[bigArray objectAtIndex:section] allValues] objectAtIndex:0] count];
}
I can't figure out this part how to implement this method based on my current data structure :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:#"MyCell"];
MyObject *obj = //Need this part
cell.textLabel.text = obj.name;
return cell;
}
Simply put, I'm trying to insert dynamic sections with dynamic data. I'm looking for advice from more experienced developers, how would you tackle this?
Assuming I understood well how your data is structured, here's how I would do it:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
//this will get you the dictionary for the section being filled
NSDictionary *item = [bigArray objectAtIndex:indexPath.section];
// then the array of object for the section
NSMutableArray *mutableArray = [item objectForKey:#"sectionName"];
//you then take the object for the row
MyObject *obj = [mutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = obj.name;
return cell;
}
Don't forget to set the reuse identifier for the cell prototype in the attributes inspector

Resources