Dynamic Grouped Table Cells - objective-c

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

Related

how cellforrowatindexpath works in objective c [duplicate]

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.

Static Table View Cell Labels are getting Squished into Header

EDIT
My constraints on custom cells are all weird. Please disregard. Just using standard Left Detail ones now.
I don't understand why in iOS8 now my static table view cell contents are collapsing into the header of each section.
There's really almost nothing in my SettingsTVC.m. This is a just a test with all static data in the storyboard:
- (void)viewDidLoad {
[super viewDidLoad];
}
Have you implemented TableView delegate methods properly i.e
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == CUSTOM_SECTION)
{
return CUSTOM_VALUE;
}
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return height;
}

UITableViewCell like mail on iphone

How can I adjust a UITableViewCell so that it would show two fields after swiping it left - identical to how the mail client does it?
Right now I only see the "Delete" button.
I would like to have the "More" field included there, too...
Also, for the first if clause Cell I do not get a "Insert" field.
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == self.reports.count){
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
Is there a way to combine the fields with the | operator or something?
Thanks, EL-
You can use undocumented delegate methods for UITableView in iOS7
- (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"More";
}
- (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self setEditing:NO animated:YES];
}
It seems that they are not private and can be submitted to appStore.

heightForRowAtIndexPath Change height variable to cell

I have three different custom cells of different heights. How should I use,
heightForRowAtIndexPath
To set the specific cell's height up. Something allong the lines of this.
if ([CellIdentifier isEqualToString:#"Cell"]) {
return 200;}
No avail
copy the code that figures out what cell to use from cellForRowAtIndexPath: to heightForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cel;
NSString *CellIdentifier;
// whatever code you use to figure out what cell to use
if ([CellIdentifier isEqualToString:#"Cell"]) {
// create & configure "Cell"-cell
}
else {
// configure Standard cell
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
// whatever code you use to figure out what cell to use
if ([CellIdentifier isEqualToString:#"Cell"]) {
return 200.0f;
}
else {
return 44.0f;
}
}

How to manage creating a UITableViewCells?

Would you guyz be so kind to help me with such question:
I have to fill the tableView with cells and I also have an array with objects to fill cells with.
The question is - how to skip creating a cell if the object doesn't meet some conditions? I mean how to write something like:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(objectIsOk) {
//create cell
}
else {
//do nothing
}
return cell;
NOTE: I don't have an access to that array of objects it gives me an object per indexPath.row dynamically
I'm unsure as to what you mean by "skip creating a cell". Your table view data source is required to return a cell for each cellForRowAtIndexPath: call, which will get called for each row in each section that you have said the table will contain.
If you want to return a blank cell then why not just do something like this:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (objectIsOk) {
// Create normal cell
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"BlankCell"];
}
return cell;
}
You could also return height of zero in the heightForRowAtIndexPath: delegate method to make it appear that it's not there, like so:
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (objectIsOk) {
return 44.0f;
} else {
return 0.0f;
}
}