How to hide empty UITableViewCells showing in UITableView with plain style - IOS? - iphone

UItableView is showing with empty rows like below...
how to hide it without changing the tableview style. Do I need to resize the table View, if so how can I know the height exactly to set.

self.tableView.tableFooterView = [[UIView alloc] init];

you add UIview in footer of uitabeview use of xib or tableview delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
return yourview;
}

What I had done in one of my projects was to set the tableview's background color to clearColor and separator to none.

Just drag and drop a view from object library to the TableView empty shell in storyboard. Make the view as footer of the table view (in document outline drag it below the cell's if it is not already at below) make the height of the view '0'. You will not see the view in storyboard because its height is '0' but when its runs it will hide the cells. I think that should work.

Why don't you put some logic into your
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
function?
Just return the number of cells which are really filled with data

Related

Custom IOS separator

How is the 'or' separator below made? is it an image used in the section header?
if that's really done with a table view, then it's done via a delegate method of "tableView:viewForHeaderInSection:".
Yes, it is an image used for section header.
That login page is modifying the header/footer views of the UITableView.
When you are setting up the table view you need use the following UITableView delegate methods in order to create a header and footer view in each section of your table view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Set your custom view for the header in the section
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// Set your custom view for the footer in the section
}
- (CGFloat) tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
// Set the height of the header in each section
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// Set the height of the footer in each section
}
NOTE: If you notice the first two methods return a UIView therefore it does not just have to be an image. You can create any custom view you want and place it in the tableView header/footer. The view could be UIKit elements, UIImage, or even made with CoreGraphics.

UISegment Control in UITableView

I have added a segment control to my table view and what my problem/question is I need to display text in tableview cells based on segment selected.
I have followed some tutorials, in that they gave me text in labels based on segment selected. I used segmentControl.selectedIndexPath also. So can anyone tell me how can we set that array of objects to my tableview cells based on segment selected?
Correct me if any mistakes in my english.
Please help me.
Thanks a lot for help.
First of all you have to divide your data in different arrays depends on your requirement ie Number of segmentControl.
Here if there are three segment controls then create three array an in the table view's delegate methods depending on segment control's selected index change the array to display in table view.
Like if segmentControl.selectedIndex == 0 then array1 if == 1 then array2 and if == 2 then array3.
In all delegate and datasource methods of table view. And on segment control's selecedIndexChange: method call reload table.
Happy Coding :)
EDIT 1
For change data in table view on segmented control's index change you must have one IBOutlet for tableView and use that IBOutlet to change the data using [tableView reloadData]; here tableView is IBOutlet for table view.
Happy Coding :)
Common use for tableview;
In Controller you have an array which has the objects, lets say _tableObjects
You have implemented in your controller datasource methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =......
.................
cell.labelText.text = [_tableObjects objectAtIndex:indexPath.row].name;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _tableObjects.count;
}
So if it is like this, you can change _tableObjects array in somewhere in you code.
_tableObjects = myOtherArray;
[self.tableView reloadData];

Can't select Cell in UITableView For UITableViewCellAccessoryCheckmark

If you have used Messages application in iOS, you know how we could invoke UITableViewCellAccessoryCheckmark in any message through edit button and then select each bubble/cell for forward or deletion purpose.
I'm trying to do the same in my application. I can tap on edit and UITableViewCellAcessoryCheckMark is shown, but I can't select the cells using it. What more do I need to implement?
Any help would be appreciated. Here is the code -
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellAccessoryCheckmark;
}
For a table view as shown in the picture, where one or more cells can be selected with a checkmark symbol, you have to set allowsMultipleSelectionDuringEditing = YES on the table view. This can be done either in viewDidLoad with
self.tableView.allowsMultipleSelectionDuringEditing = YES
or in the Attributes Inspector of the table view in the NIB/Storyboard file by setting "Editing" to "Multiple Selection During Editing".
The tableView:editingStyleForRowAtIndexPath: method is not needed for this.
(And btw your method returns UITableViewCellAccessoryCheckmark which is a UITableViewCellAccessoryType and not a UITableViewCellEditingStyle.)

Fixed button at bottom of UITableView

I have a view controller which has a tableview subview. I have made the tableview height smaller, leaving about 46 pixels for a button that needs to stay at the bottom and not scrolled with the table.
I have played around changing the height of the table view (in IB) and it doesn't seem to change anything. Any help would be appreciated.
You need to have the UIButton at the same level as the UITableView - both are subviews of the main view. Do you have the UITableView as the view of the view controller?
This can be done programmatically in the UITableViewDelegate:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// Build a custom view
// ...
// Build a custom button
// ...
[customFooterView addSubview:button];
return [customFooterView autorelease];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 53.0; // return your button's height
}

How to get the size of the "Delete" button in a UITableViewCell when swipe to delete?

I add a datetime label on the right of a table cell. When swip-to-delete shows the "Delete" button, the datetime label need to shift left a little bit. But how to get the "Delete" button's size?
I tried to find it in the cell.subviews but failed.
You don't have to know the button's size. Instead, use the size of the cell's contentView property to calculate the sizes of the subviews. When swiping over a cell, UIKit will adapt the contentView's size and call layoutSubviews on the cell-object. In your subclass of UITableViewCell, overwrite the layoutSubviews method and set the appropriate sizes to the subviews.
Look at RecipeTableViewCell.m of Apple's iPhoneCoreDataRecipes sample code.
Use this code in your custom Cell class
- (void)layoutSubviews {
[super layoutSubviews];
NSMutableArray *subviews = [self.subviews mutableCopy];
while (subviews.count > 0)
{
UIView *subV = subviews[0];
[subviews removeObjectAtIndex:0];
if ([NSStringFromClass([subV class])isEqualToString:#"UITableViewCellDeleteConfirmationView"])
{
UIView *deleteButtonView = (UIView *)[self.subviews objectAtIndex:0];
CGFloat deleteBtnHeight=deleteButtonView.frame.size.height;//here you get the height
}
}
}
The size adjusts to fit the text contained. See the following code:
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"Dynamic width!";
}
vs
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return #"⏎";
}
If you don't override layoutSubviews method in your custom table view cell than my approach is:
Create your custom subview, set frame basing on contentView.bounds.
Set autoresizingMask to UIViewAutoresizingFlexibleWidth.
Add your custom subview to ContentView of a cell.
Configure cell for editing
Now when you swipe on cell the delete button appears and your view auto resizes with contentView.
The delete button is 63x33.