Custom empty cells in UITabelView - ios

i got ios application with UITableView
I customize cells in this table, but when number of objects lower than visible cells on screen, all other cells look like simple UITableViewCells.
It should look like this
How should i customize all cells?
UPD Code for dataSource:
pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [dataBase.showMaps count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ShowMapCell *cell = (ShowMapCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ShowMapCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
ShowMap *sM = [dataBase.showMaps objectAtIndex:indexPath.row];
//cell.textLabel.text = sM.name;
[cell initShowMapCell:sM index:indexPath.row];
return cell;
}

You could have just set background color of the tableview. However that solution wont work because you are using different backgrounds for each cell.
So according to me to achieve what you want you need to add some extra (blank) rows to the table and for that you need to return your record + extra cell (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method. And above that you need to set the background color of table view to one of the cell background because if you are using bouncing effect in the tableview you will find white color when you scroll the tableview up/down.

If you want your UITableView not to have 'empty cells', you will need to hide the cell separators and change the background colour of the tableview to match your colour scheme.
As katzenhut said in the comments, they aren't cells, they are placeholders for content. So if you hide the separators and add them in the xib file for your UITableViewCell ShowCell, then they will no longer be seen when there isn't a cell to place in the table.

Related

IOS - Add Cells at particular index with uistepper

I am displaying dynamic data in UITableView with checkbox button in it. When User Select Checkbox button it will show UIStepper just below it. I dont know how to achieve this, what I exactly required is mentioned in the url below.
Target To achieve
I have just displayed the data in UITableView yet as mentioned in the code below. I don't know how to achieve this whether should i add cells below every index when user select checkbox or there some other good approach that i should follow.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [getNutrients count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NutrientCell";
UITableViewCell *nutrientCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
nutrientCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture_bg.png"]];
UILabel *nutrientLabel = (UILabel*)[nutrientCell viewWithTag:1];
nutrientLabel.text = [[getNutrients objectAtIndex:indexPath.row]valueForKey:#"name"];
return nutrientCell;
}
UITableView has property to insert row or section.
You can use insertRowsAtIndexPaths:withRowAnimation:
deleteRowsAtIndexPaths:withRowAnimation:
For insert and delete row respectively.

If we want static cell as well dynamic cells in same table view controller ios, how it can be possible in ios?

I have a tableViewController, and under that i want one static cell and rest of all will be dynamic cells . I have already run for dynamic cells , but within same tableViewController i also need to add one static cell, how can i achieve it?
Please Help
Thanks in advance.
you could do something like the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dynamicContent.count + 1; // +1 for the static cell at the beginning
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// static cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"StaticCellIdentifier" forIndexPath:indexPath];
// customization
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DynamicCellIdentifier" forIndexPath:indexPath];
id contentObject = self.dynamicContent[indexPath.row];
// customization
return cell;
}
You cannot create static and dynamic cell at same time in a UITableViewController.
But you can hard code your static cell's data and load the data each time you reload your tableview.
You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them.
typedef NS_ENUM(NSUInteger, TableViewSectionType) {
TableViewSectionType_Static,
TableViewSectionType_Dynamic
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; // One for static cell, and another for dynamic cells
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
switch(section) {
case TableViewSectionType_Static:
return 1; // Always return '1' to show the static cell at all times.
case TableViewSectionType_Dynamic:
return [myDynamicData count];
}
}
With this approach your cells will be split into two sections and it will be easier to manage. And it will always show one cell, as number of rows returned for TableViewSectionType_Static is 1 always. It will show the dynamic cells based on your data count.

Add a cell to the bottom of UITableView that only appears when there is no more content

So I want to add a cell to the bottom of a UITableView that only appears when there is no more content.
The goal is that when there is no more users in the list I want to have a cell that shows up with Twitter, Facebook etc buttons so the user can invite more users. Any hints?
All the best
Start off by creating different NSTableViewCell's - one for your regular data and one for your "Social Media" cell at the end.
In the UITableViewDataSource you can report the number of rows as being one greater than the total number of rows for your data
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mDataSource count] + 1;
}
Then, in your cellForRowAtIndexPath method, check if it's the last row, and return your "social media" cell.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
NSString *cellIdentifier = #"Cell";
if (indexPath.row == [mDataSource count] + 1) {
cellIdentifier = #"SocialCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
...
}

First cell always empty in table. Display only from the second cell - Objective c

I have a table view which display the contacts from array. I setup the table view delegates by follows.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [contactArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [contactArray objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}
But the first cell in the table view always empty. It starts display only from second cell. I thought it may be header view. So I removed the header using the following delegate methods.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0;
}
But still have the problem. I attached the screenshot about this issue.
Any help will be appreciated.
Your TableView is fine and it is working correctly, this is due to some other problem that is included in iOS 7, that automatically scroll insets. To solve this problem, go to your storyboard and select the viewcontroller in which your TableView is and select the ViewController and select the Properties of that ViewController, and uncheck this checkbox, which is read as Adjust ScrollView Insets. See this screen shot,
Your table is correct.Just your table was auto adjusted by the viewController.
You can write self.automaticallyAdjustsScrollViewInsets = NO;
Your Deduction is wrong your first cell isn't missing, but your tableview has started by 64 points down. So change your frame of your tableview or your tableview constraints accordingly.
Tip : Try setting a background colour when you have to debug things like this to clear your doubts.

Adding sections in a grouped table view in Xcode

My code is a little different to others, but it works.
I am new to app coding, but I would like to add some of
these into sections:
So some of them have their own group with a small title to each.
But my code looks like this:
and I don't know what to insert to do it right.
(The bottom half of that picture is the pictures in the detail view, that shows up in the detail view when you select something from the table view.)
(I know Xcode shows errors in my code, but it still works. )
Can anyone help me?
You have to implement some UITableView methods and delegate methods (and of course set your class as the delegate of the class) :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Here you must return the number of sectiosn you want
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Here, for each section, you must return the number of rows it will contain
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//For each section, you must return here it's label
if(section == 0) return #"header 1";
.....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.text = // some to display in the cell represented by indexPath.section and indexPath.row;
return cell;
}
With that, you can arrange your data as you want : One array for each section, one big array with sub arrays, ... as you want. Antthing will be ok as far as you can return the wanted values from the methods above.

Resources