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
Related
I have created an iOS app with UITableView.UITableView by default loads 25 records. I am scrolling the tableview to get another 25 records.
How do I get another set of data?
Here is my code.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return timeList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Item";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tmpDict = [timeList objectAtIndex:indexPath.row];
NSMutableString *text;
NSString *currentstatus;
text = [NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:startdate]];
currentstatus = [self statusString:[NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:status]]];
NSMutableString *detail;
detail = [NSMutableString stringWithFormat:#"User:%# , Hours:%# , Status: %# ",
[tmpDict objectForKey:username], [tmpDict objectForKey:hours], currentstatus];
cell.textLabel.text = text;
cell.detailTextLabel.text= detail;
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *tmpDict = [timeList objectAtIndex:indexPath.row];
NSString * storyboardName=#"Main";
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:storyboardName bundle:nil];
EditViewController *evc =[storyboard instantiateViewControllerWithIdentifier:#"EditViewController"];
evc.startday =[NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:startdate]];
evc.user_id = [NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:user_id]];
evc.tab =#"wktime";
[self presentViewController:evc animated:NO completion:Nil];
}
First you have check whether the tableview reached last cell or not. If it is reached then again you have to reload the next set of data.
Use the below code to check whether the tableview reached last cell or not.
-(void)tableView:(UITableView *)tableView willDisplayCell (UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == timeList.count-1)
{
//Your code here
}
}
You can use delegate method of UISCrollView scrollViewDidEndScrolling to load more data and then you can refresh your tableView.
You can do it in a simple way. by adding below code in cellForRowAtIndexPath delegate method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == timeList.count-1) {
NSLog(#"load more");
}
.....
return cell;
}
if condition will be executed at the end of tableview, there you can add your logic to load more data's.
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 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];