I don't know what to think...
I set up my main view to contain a UITableView. (I am using a UIPickerView as well on that view.)
I defined it to conform to the protocols:
UIPickerViewDelegate,
UIPickerViewDataSource,
UITableViewDataSource,
UITableViewDelegate
Announced the two methods in my view's h:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
implemented them in m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
cell.textLabel.text = [NSString stringWithFormat:#"Test %d",indexPath];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
I did set the delegate in my viewDidLoad:
searchTableView = [[UITableView alloc] init];
[searchTableView setFrame:CGRectMake(searchBar.frame.origin.x+10, 50, searchBar.frame.size.width-20, 300)];
searchTableView.delegate=self;
And yet, not numberOfRowsInSection nor cellForRowAtIndexPath are ever called!
What am I doing wrong ?
set the datasource:
[searchTableView setDataSource:self];
Related
I embedded the navigation controller to the UITableViewController, and for every cell I need click the cell to push to another viewcontroller. The problem is when I enable the animation, it would be very laggy clicking the cell. I don't know why.
The code is following as:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = #"test";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SliderViewController *vc = [[SliderViewController alloc] init];
[self.navigationController pushViewController:vc animated:NO];
}
I think it is quite normal so I don't know what's the problem.
I have a problem and cellForRowAtIndexPath is not being called. I checked that both the no of row and sections are not zero. Thanks in advance for any advise.
- (void)viewDidLoad
{
[super viewDidLoad];
MakeupTable=[[UITableView alloc]init];
MakeupTable.dataSource=self;
MakeupTable.delegate=self;
[MakeupTable reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return 4;
}else{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
return cell;
}
Make sure this class is set as Table view delegates:
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
What is the easiest way to send the data stored in an NSArray to a NSTableView and display it line by line?
For example:
NSArray has data [a, b, c]
I want the NSTableView to say:
a
b
c
The NSTableView only needs 1 column.
You don't "send" things to NSTableView. NSTableView asks you for things. It does so via the NSTableViewDataSource protocol. So all you need to do is implement the two required methods from that (-numberOfRowsInTableView: and -tableView:objectValueForTableColumn:row:), and connect the tableview's data source outlet to your object.
Documentation for NSTableViewDataSource is here: https://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html
You need to explore UITableViewDelegate and UiTableViewDataSource delegate methods:
#pragma mark --- Table View Delegate Methods ----------------------------
//Handles the selection of a cell in a table view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//Defines the number of sections in a table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//Defines the header of the section in the table view
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return nil;
}
//Defines the number of rows in each section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
//Defines the content of the table view cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [myDataArray objectAtIndex:[indexPath row]];//<-pay attention to this line
return cell;
}
I have a very simple tableview in a xib file with it's delegate and datasource hooked up to TestVC. TestVC is simple:
#import "TestVC.h"
#implementation TestVC
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IDEN = #"IDEN";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:IDEN];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDEN];
}
cell.textLabel.text = #"test";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (void)tableView:(UITableView *)_tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"called");
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
When I click on a cell, it doesn't get deselected. I even put a log statement there to see if didDeselectRowAtIndexPath method is being called and it is.
What am i doing wrong?
You are using the wrong method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is the one you need
My UITableView is not populated because:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
is never invoked.
This is how I initialize the table:
self.recentScannedItems = [[[UITableView alloc] initWithFrame:kLastScannedPortrait style:UITableViewStylePlain] autorelease];
[self.recentScannedItems setDelegate:self];
[self.recentScannedItems setDataSource:self];
[self.view addSubview:recentScannedItems];
what am I missing ?
Have you implemented correctly the datasource methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return NumberOfSections;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return NumberOfRows;
}
If you are not implementing this methods, or if you are returning 0 in them, cellForRowAtIndexPath won't be called
Does your class comply to UITableViewDataSource? Like:
#interface mySuperClass : UIViewController <UITableViewDataSource, UITableViewDelegate>