开发者

Array to UITableView

This sounds like it's easy and from the tutorials it looks very easy.

I have followed the tutorials from their words EXACTLY and still can not get the array to display on my UITableView.

Here is my code.

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
   salesArray = [[NSMutableArray alloc] init];

  //Add items
  [salesArray  addObject:@"Credit"];
  [salesArray  addObject:@"Debit"];
  [salesArray  addObject:@"EBT"];


  //Set the title
  self.navigationItem.title = @"Sale Type's";
}

-(NSInteger)tableView:(UITableView *)tableView 
                      numberOfRowsInSection:(NSInteger)section
{return [salesArray count];}

-(UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if(cell == nil)
  {
    cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero 
    reuseIdentifier:CellIdentifier]autorelease];
  }
  NSString *value = [salesArray objectAtIndex:indexPath.row];
  cell.textLabel.text = value;
  return cel开发者_JAVA百科l;
}

And yes, I have declared it in the .h file and the .m file.

Any help in any directions would be great, thank you!


Declare your salesArray as your property and in the the cellForRowAtIndexPath, use

cell.textLabel.text = [self.salesArray objectAtIndex:indexPath.row];


I am assuming that your view controller extends UITableViewController? If so, you just need to add this line at the end of your viewDidLoad and/or viewDidAppear methods:

[self.tableView reloadData];

Also, I would use self.salesArray instead of salesArray. Not exactly sure what the difference is, but I have had issues in the past not prepending "self" to some of my variables.


Your code seems to be ok but I think you forgot to change the number of sections from zero to one.

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


In my point of view it's much better and easily to understand according to the code below:

import Foundation
import UIKit
class testTableViewController: UITableViewController {    
override func viewDidLoad{
super.viewDidLoad()
}

let nameList = ["name1","name2","name3","name4","name5","name6"]

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int)->
Int{
return nameList.count
}

override func tableView(TableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("test_row", forIndexPath: indexPath)
let row1 = nameList[indexPath.row]

cell.textLabel? .text = row1
return cell
}

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜