I have a scroll view on my UIViewController. And on my scroll view I added a Table View and a Table View Cell on it. I used the same code I previously used for my UITableViewController but nothing appears on the Table View. What can I do to fix it?
Here is my code:
NSString *const kReward = #"reward";
NSString *const kPoints = #"points";
NSString *const kPicture = #"picture";
#interface RewardsVC ()
#end
#implementation RewardsVC
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *rewardOne = #{kReward: #"gjshfkasjhdf",
kPoints: #"1sfasf",
kPicture: #"1.jpg",
};
NSDictionary *rewardTwo = #{kReward: #"df",
kPoints: #"dfas",
kPicture: #"asdf.jpg",
};
NSDictionary *rewardThree = #{kReward: #"asdf",
kPoints: #"asdf",
kPicture: #"sadfsdf.jpg",
};
NSString *stringPlaceHolder
NSArray *rewardsArray
rewardsArray = [NSArray arrayWithObjects:
rewardOne,
rewardTwo,
rewardThree,
nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [rewardsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *rewardItems = [rewardsArray objectAtIndex:indexPath.row];
// Item Name
cell.textLabel.text = [rewardItems objectForKey:kReward];
// Item Price
cell.detailTextLabel.text = [rewardItems objectForKey:kPoints];
// Item Image
stringPlaceHolder = [rewardItems objectForKey:kPicture];
cell.imageView.image = [UIImage imageNamed: stringPlaceHolder];
return cell;
}
In UITableViewCintroller, there is no need to create a table and to set delegate or datasource on it. becouse thats class's defoult fuctinality. but when you are tring to create a tableview on UIViewController's view. you have to Alloc a Tableview and have to set its frame, then set its delegate by either Interface builder window(if you are creating it by XIB or Storyboard) or by code.
I assume that you know how to set frame.
and for delegate.
just Call UItableviewDelegate and UItableviewDatasorce in your .h file
and set in viewDidLoad after allocation
tableview.delegate = self;
tableview.datasource = self;
and for Iinerface builder ..
add UITableViewDelegate & UITableViewDataSource
#interface RewardsVC : UIViewController <UITableViewDelegate, UITableViewDataSource>{
}
#property (nonatomic, strong) UITableView *tableView;
#end
set delegate and dataSource in viewDidLoad after init tableview
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,400,600)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
Related
First of all I want to apologize for my bad english.
I'm having trouble to set the properties of my custom UITableViewCell (HistoricoCell).
When I try to set a property of my cell I get: Signal SIGABRT error:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
// Fetch Item
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row];
// Configure Table View Cell
[cell.lblCodigo setText:[NSString stringWithFormat:#"%#", item[#"codigo"]]];
[cell.btnFavoritar addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
I followed a lot of tutorials and questions on the web but I stil with my error.
Can someone help me?
My code:
HistoricoCell.h
#import <UIKit/UIKit.h>
#interface HistoricoCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *lblCodigo;
#property (weak, nonatomic) IBOutlet UIButton *btnFavoritar;
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tblHistorico;
SecondViewController.m
#import "SecondViewController.h"
#import "DBManager.h"
#import "HistoricoCell.h"
#interface SecondViewController ()
#property (nonatomic, strong) DBManager *dbManager;
#property (nonatomic, strong) NSArray *arrPeopleInfo;
-(void)loadData;
#end
#implementation SecondViewController
static NSString *CellIdentifier = #"CellIdentifier";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Make self the delegate and datasource of the table view.
self.tblHistorico.delegate = self;
self.tblHistorico.dataSource = self;
// Initialize the dbManager property.
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:#"bernoullidb.sql"];
[self.tblHistorico registerClass:[HistoricoCell class] forCellReuseIdentifier:#"CellIdentifier"];
[self loadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)loadData{
// Form the query.
NSString *query = #"select * from tbHistorico";
// Get the results.
if (self.arrPeopleInfo != nil) {
self.arrPeopleInfo = nil;
}
self.arrPeopleInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
// Reload the table view.
//[self.tblHistorico reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arrPeopleInfo.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
// Fetch Item
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row];
// Configure Table View Cell
[cell.lblCodigo setText:[NSString stringWithFormat:#"%#", item[#"codigo"]]];
[cell.btnFavoritar addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)didTapButton:(id)sender {
NSLog(#"%s", __PRETTY_FUNCTION__);
}
#end
You should set cell indentifier "CellIdentifier" for your cell in File Inspector
Or register your nib file if you add cell with nib:
UINib *itemNib = [UINib nibWithNibName:#"yourCell" bundle:nil];
[self.tableView registerNib:itemNib forCellReuseIdentifier:#"yourCellReuseIndentifier"];
I think your problem is in your cell creation: you try to dequeue a cell if it exists (i.e. recycle a previously used cell). that is OK, but, especially when the TableView is displayed for the first time, no previously used cell for this table exists. So, you have to create one if the dequeueReusableCellWithIdentifier call return nil.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Dequeue the cell.
HistoricoCell *cell = (HistoricoCell *)[self.tblHistorico dequeueReusableCellWithIdentifier:#"HistoricoCellIdentifier" forIndexPath:indexPath];
if( cell == nil ) // no queuded cell to dequeue
{
// you have to create a fresh new one
cell = [HistoricoCell alloc] initWithStyle:<your cell style> reuseIdentifier:#"HistoricoCellIdentifier"];
}
// Fetch Item
NSDictionary *item = [self.dbManager.arrColumnNames objectAtIndex:indexPath.row];
// Configure Table View Cell
[cell.lblCodigo setText:[NSString stringWithFormat:#"%#", item[#"codigo"]]];
[cell.btnFavoritar addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Having an issue where the array values do not display in my tableview cells, but can be printed out correctly with NSLog. Thanks in advance for your help!
TableViewCell .h
#import <UIKit/UIKit.h>
#interface TableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *image;
#property (strong, nonatomic) IBOutlet UILabel *label;
#end
TableViewController.h
#import <UIKit/UIKit.h>
#interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
#property(nonatomic, strong) NSMutableArray *imagesArray;
#property(nonatomic, strong) NSMutableArray *namesArray;
#end
TableViewController.m
#import "TableViewController.h"
#import "TableViewCell.h"
#interface TableViewController ()
#end
#implementation TableViewController
#synthesize primaryWeaponNames = _primaryWeaponNames;
- (void)viewDidLoad {
[super viewDidLoad];
[self setupArrays];
}
- (void)setupArrays {
_namesArray = [[NSMutableArray alloc] initWithObjects:
#"NAME1", #"NAME2", #"NAME3"
nil];
self.imagesArray = [[NSMutableArray alloc] initWithObjects:
#"IMG1", #"IMG2", #"IMG3"
nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return _namesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.nameLabel.text = [NSString stringWithFormat:#"%#", [_namesArray objectAtIndex:indexPath.row]];
NSLog(#"%#", _namesArray[indexPath.row]);
cell.image.image = [self.imagesArray objectAtIndex:indexPath.row];
[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
When you make the cell in a xib file, you should register the nib in viewDidLoad. Since you didn't do that, the dequeue method will return nil, and you're just creating a default cell (in the if (cell == nil) clause) instead of your custom cell. The default cell doesn't have a nameLabel or image property, so the lines to set the values of those outlets won't do anything.
[self.tableView registerNib:[UINib nibWithNibName:#"Your nib name here" bundle:nil] forCellReuseIdentifier:#"TableViewCell];
In your TableViewController class, you need to include
self.tableView.delegate = self;
self.tableView.datasource = self;
in your viewDidLoad method. Or you can do this in interface builder by right click dragging the table view in your tableview controller to file's owner and setting delegate, then repeat for datasource
I'm trying to create a table but none of the data I inputted is displaying when I run the simulator. I'm trying to create a simple address book application and So far, here is what I did:
From the empty story board, I added a View Controller from the Object Library then inside the view controller, I added a Table View. Then I embedded the View Controller inside a Navigation Controller. I also added another View Controller in the end to act as a "Details" View.
Inside my viewController.h I added the following code:
#property (strong, nonatomic) NSArray *contact;
#property (strong, nonatomic) NSArray *contactDetails;
#property (nonatomic) NSInteger selectedIndex;
contact is to act as the array that shows all the contacts within the phonebook (This is what I'm having trouble displaying)
contactDetails shows once a user selects a contact.
selectedIndex is my way of telling the application which contact has been selected.
Inside my viewController.m I added the following code:
#import "HWViewController.h"
#interface HWViewController ()
#end
#implementation HWViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerClass: [UITableViewCell class] forCellReuseIdentifier:#"Cell"];
self.contact = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects:#"Joe", #"Simon", nil]];
self.contactDetails = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects: #"+689171898057", #"+689173104153", nil]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.contact.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if(cell==nil){
cell = [[[NSBundle mainBundle] loadNibNamed:#"SampleCell" owner:nil options:nil] objectAtIndex:0];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
label.text = [self.contact objectAtIndex:indexPath.row];
[cell.contentView addSubview:label];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.selectedIndex = indexPath.row;
[self performSegueWithIdentifier:#"TranslationSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
UIViewController *vc = [segue destinationViewController];
UILabel *details = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 250, 32)];
details.text = [self.contactDetails objectAtIndex:((HWViewController*)sender).selectedIndex];
[vc.view addSubview:details];
}
#end
After that, I ran the simulation and I get nothing showing up on the table.
you have no cell in the tableview. so add tableview cell in your storyboard
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Add this in your viewController.h :
#property(nonatomic, strong) IBOUTLET UITableView * TableView;
And then link this outlet to your tableview and then try it out.
And there is no cell in your table view because the prototype cell will be set to zero by default. Now select the tableview go to attribute inspector and set the prototype cell to 1 and then try to run your program with proper cell identifier it will work.
HTH :)
Set tableView delegate and DataSource protocol in you viewdidload method.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.tableView registerClass: [UITableViewCell class] forCellReuseIdentifier:#"Cell"];
self.tableview.delegate = self;
self.tableview.dataSource = self;
self.contact = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects:#"Joe", #"Simon", nil]];
self.contactDetails = [[NSArray alloc] initWithArray: [NSArray arrayWithObjects: #"+689171898057", #"+689173104153", nil]];
}
How might I pass my objects via parameters from my custom UITableViewCell class to my UITableView. Because I'm using Dynamic Prototype Cells, I can't assign my objects to the textviews within the cells. For this reason I need to pass my objects from class to class.
MainTableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myURL = [[NSURL alloc]initWithString:#"http://domain.com/json2.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
if(!error)
{
for (NSDictionary * needs in jsonArray)
{
textNeedTitle.text = [needs objectForKey: #"needTitle"];
textNeedPoster.text = [needs objectForKey: #"needPoster"];
textNeedDescrip.text = [needs objectForKey: #"needDescrip"];
}
}
else
{
textNeedTitle.text = [NSString stringWithFormat:#"Error--%#",[error description]];
}
return cell;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
TestTableViewCell.h
#import <UIKit/UIKit.h>
#interface TestTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextView *textNeedTitle;
#property (weak, nonatomic) IBOutlet UITextView *textNeedPoster;
#property (weak, nonatomic) IBOutlet UITextView *textNeedDescrip;
Each of the properties above are assigned via a Referencing Outlet to it's appropriate UITextView.
I'm not entirely sure what you're trying to do, but I noticed that in your UITableViewController you're getting a UITableViewCell instead of your custom class TestTableViewCell.
It should be like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = (TestTableViewCell *) [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
}
Make sure that:
* In the storyboard, the reuse identifier of the cells is "Cell" (or a more appropriate name...)
* The class for the prototype cells is TestTableViewCell
Then you should be able to access the properties of the cell, like cell.textNeedTitle.text = #"Something"
There are 5 objects in datasoure for example,if the first object is like this:
Obj -> id:1,name:"A"
when I change the object's name to "B";
Obj -> id:1,name:"B"
then [tableView reloadData]
the first cell still display "A",I want to change it to "B".
In the cellForRowAtIndexpath method manage the datasource method properly as it retrives the value from the datasource array and displays it,That is it
I doubt the problem is that the reusability is causing the trouble in your code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *cellIdentifier1 = #"surveyCell";
if (tableView== self.surveytableView) {
cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if(cell==nil)
{
//alloc the cell
//DO NOT SET THE VALUE HERE
}
//here set the value
return cell;
}
here is the code, what i did was,
created a class called "DataClass" which provides data for your tableview
created objects like you mentioned , i stored it in an array("dataSource")
after that i loaded it to tableview (i assume u are properly wired up tableview datasource and delegate)
I put a button to make change the string in the datasource array.
button's action is connected to method and within that i am reloading the tableview
//class DataClass
#interface DataClass : NSObject
{
#public;
NSString *str;
}
#implementation DataClass
- (id)init
{
[super init];
str = nil;
return self;
}
#end
//viewController.h
#interface ViewController : UIViewController<UITableViewDataSource ,UITableViewDelegate>
{
IBOutlet UITableView *aTableView;
IBOutlet UIButton *aButton;
}
- (IBAction)whenButtonClicked:(id)sender;
#end
//in .m file
#import "ViewController.h"
#import "DataClass.h"
#interface ViewController ()
{
NSMutableArray *DataSource;
}
#end
// ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aTableView.dataSource = self;
aTableView.delegate = self;
DataSource = [[NSMutableArray alloc]init];
for(int i=0 ; i<5 ; i++)
{
DataClass *data = [[DataClass alloc]init];
data->str=#"hello";
[DataSource addObject:data];
[data release];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(aCell == nil)
{
aCell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"]autorelease];
}
DataClass *aValue = [DataSource objectAtIndex:indexPath.row];
aCell.textLabel.text = aValue->str;
return aCell;
}
- (IBAction)whenButtonClicked:(id)sender
{
DataClass *aObj = [DataSource objectAtIndex:2];//changing the 3'rd object value as yours in 5th object
aObj->str = #"world";
[aTableView reloadData];
}
#end
//after "changeValue" button pressed the third row displays "world"