how cellforrowatindexpath works in objective c [duplicate] - objective-c

This question already has answers here:
How does cellForRowAtIndexPath work?
(3 answers)
Closed 5 years ago.
I am new to objective-c programming language.I create a table and create all method of table View .But i don't understand about CellForRowAtIndexPath.Please tell me some one how it work.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This is delegate method of UITableView. The returned object is of type UITableViewCell. These are the objects that you see in the table's rows. NSIndexPath has two this Section and Row.
It is called if you implement the UITableViewDataSource protocol in your view controller. A simpler way would be to add a UITableViewController class. I strongly recommend this because it Apple has some code written for you to easily implement the functions that can describe a table. Anyway, if you choose to implement this protocol yourself, you need to create a UITableViewCell object and return it for whatever row. Have a look at its class reference to understand re-usablity because the cells that are displayed in the table view are reused again and again(this is a very efficient design btw).
If your implementing custom cell then I will strongly recommend you use
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to return only empty cell not set here. use thing like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCart_Cell" forIndexPath:indexPath];
return cell;
}
and the use this delegate which will called just after cellForRow data source method
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell isKindOfClass:[CartTableViewCell class]])
{
CartTableViewCell *cell1 = (CartTableViewCell*)cell;
MyCartModel* data = [_myCartProductArray objectAtIndex:indexPath.row];
[cell1 setUpData:data];
}
}
and set data on UILabel in UITableviewcell custom class.

Related

How to click only one cell at a time like radio button in objective c

I am new in iOS and I am facing problem regarding to select one cell and on click of them change image. But when I click on other cell its image also got change.
As in image I can select all Full,Partial and None.I want to select only one at a time like radio button.
My code is like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FPNTableViewCell *cell = (FPNTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.circleimg.image=[UIImage imageNamed:#"CircleClick.png"];
}
I am using Custom tableview. Thanks in Advance!
Initialise an Int (top of the controller Eg: int i=0;)
tableview didselect method assign the selected index to that int
EG: i = indexpath.row
Cellforrowindex method check the i with indexpath. Based on that you can use the image
You can use tableview method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
for selecting.
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
(NSIndexPath *)indexPath {
}
for unselect.

How to always show tableView cell when i scroll

I need to always show cell with segmentControll, how can i do it?
There are a couple of ways to do this. Here is what I would do.
1
Don't put the segmented control as part of the table. Simply move it outside the UITableView. With that being said, if you are using a TableView Controller, you have to switch to a ViewController and add TableView to it.
2
This is a way to create a header for your table. Maybe that's what you want.
Use this method if you want a title:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Or this one if you want a custom view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
And for the height:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

ipad add tableview (only a part) to viewcontroller with storyboard

So I'm trying to add a UITableView on the lower half of my ipad app which will be used to display a search result. This is how I did it.
I added a UIView
I added a UItableView onto the UIView
I then dragged the UITableView to the ViewController so it can connect to it for delegate and datasource.
This is what it currently looks like:
(It's at that middle top row)
So I added the following onto the viewcontroller class to generate the data
# pragma mark TableView properties
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"SearchResultCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
}
The debugger would go through all these but would get stuck after the "cellForRowAtIndexPath" method:
It would just go through that and would not end until I stop the whole debugging. Not really sure what's going on.
Thoughts? Maybe you guys can point me to the right direction as to how I should generate my search results.
Thanks!
I usually find it much more faster and easier to use the free Sensible TableView framework to do automatic table view searches, instead of using the regular datasource/delegate system which I could never get right.

Dynamic Grouped Table Cells

In my storyboard, I have a UITableView and it has the following Properties
Style: Grouped
Content: Dynamic Prototypes
My question was how to make the different groups. When I run the app right now, there is no white backing (Only the default lined backing) with the text placed on top. Also, if I then created these dynamic groups, how would I set their header and footer?
Thanks
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// implement your code
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//implement your code
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/// Write basic code//
switch (indexPath.section) {
case 0:
//It belongs to first group and implement your code.
break;
case 1:
//It belongs to second group and implement your code.
break;
}
I think it will be helpful to you

How to populate the rootviewcontroller tableview with an NSMutableArray

I am trying to populate the tableview with an NSMutableArray, and I am having a really hard time with it.
This should be simple I'm thinking, but I can't get it to work.
Any help would be appreciated.
Thanks
You simply have to create an object implementing the UITableViewDataSource.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.yourArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/* create your cell here */
AnObjectFromYourArray *myObject = [self.yourArray objectAtIndex:[indexPath row];
/* fill the cell with the info in my object */
return yourCell;
}
simple as that.
Greg