How to manage creating a UITableViewCells? - objective-c

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;
}
}

Related

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 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

Changing in Code Specific Row Heights in Objective-C

I have a UITableView that uses prototype cells to recreate a table. I have the first row with height 30 and the rest default 44. I would like to change only first row's height but have failed to do so. I tried cellForRowAtIndexPath but I learned here that it's heightForRowAtIndexPath: I'm interested in. However, all the IndexPath's that get sent to it are [0,0]. Can anyone help?
Have you tried this?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) { //first row
return 30;
}
else {
return 44;
}
}
Do this for resizing the cells in your table views delegate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return 30.0;
return 44.0;
}
If this doesn't work: give us your code