How to set swipe to delete UITableview frame height in swift - ios

The Swipe delete button shows depends on TableviewCell Height.
Need to reduce the height of the delete button.
Can anyone help me please?

It is not a good practice to change native controls, but you still can do it by subclassing UITableViewCell
#implementation UITableViewCellSubclass
- (void)layoutSubviews {
[super layoutSubviews];
if (self.showingDeleteConfirmation) {
if ([self.subviews count] < 4) return;
UIView *deleteButton = [self.subviews objectAtIndex:3];
deleteButton.frame = CGRectOffset(deleteButton.frame, 10, 10);
}
}
#end
But, it is very bad way to handle it. Better create custom UITableViewCell with custom behaviour and custom delete UIButton and then do whatever you want with it.

Related

Image on cells not all the way to the left

I am learning about UITableview on iOS and following a course online. I get the table showing fine, but the images on my cells are not all the way to the left (whereas the instructor's ones are). Here is a screenshot of the cells in question:
I don't want that gap, I want the images to be positioned right at the beggining of the cell, all the way to the left. I have done some research and it seems Apple has changed the default look of the cells between ios6 and ios7 so that now the images in cells show a little gap at the left. To get rid of it, I have tried UIEdgeInsets:
[tableView setSeparatorInset:UIEdgeInsetsZero];
and that's not working. I also have tried this approach:
cell.imageView.frame = CGRectMake( 0, 0, 50, 55 );
Nothing happens. So how would I go about it? Thanks
edit-------------------------------------------------------------------------------------------------------------------------------
Still not have found the answer to this. The solutions posted here don't work. I found this piece of code:
self.tableView.contentInset = UIEdgeInsetsMake(0, -50, 0, 0);
Which besides completely puzzling me (as the parameter affected should be the y?) I thought solved the issue by making the image on the cell appear all the way to the left, until I realised it only moved the whole view to the left (as I should have expected I guess) leaving an equal gap on the other side of the screen. All I want is for my images in the cells to appear all the way to the left of the cell as it used to be the case on previous ios. Thanks
It happens because default table content offset from left is 15, you should change it with 0.
See this once, you get idea Remove empty space before cells in UITableView
If you create custom cells. UITableViewCell have owner imageView. Change title of image in your cell.
If you use default cell, use custom cell with constraint Leading space = 0.
It is better not use default imageView of the cell. Drag and drop UIImageView from objective library, create a custom table view cell (Child class of UITableViewCell) then create and outlet of the image view just dragged.
The spacing in the UITableViewCell is because of the default TRUE returned by shouldIndentWhileEditingRowAtIndexPath method of UITableViewDelegate.
I was able to reproduce your problem by the below scenario:
UITableView is in editable mode:
self.tableView.editing = true
And you have implemented:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
To correct your code:
If you do not want to set Editing Style then you can turn off the editing mode by
self.tableView.editing = false
and remove editingStyleForRowAtIndexPath.
Else if you need editing mode then set the appropiate Editing style(UITableViewCellEditingStyleDeleteor UITableViewCellEditingStyleInsert) or simply turn the indentation off.
- (BOOL)tableView:(UITableView *)tableView
shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return FALSE;
}
You must create a custom cell, by adding a new class as a subclass of UITableViewCell. then you can design cell with autolayout and constraints which will resolve the issue.
there is a another concrete way to achieve this by creating subclass uitableviewcell (custom class).
steps to follow
create a class subclass of UITableViewCell.
in .h file create properties and outlets of UI components.
go to storyboard and add table view cell inside the tableview.
now add UI components like: imageview or button etc and set the x, y values according to.
make class of custom cell your className using identity inspector see image.
connect all outlets of UI components.
use below code uitableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *MyIdentifier = #"uniqueIdentifire";
yourCustomClassForCell *cell = (yourCustomClassForCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[yourCustomClassForCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.imageView.image = [imageAry objectAtIndex:indexPath.row];
}
Dont forget to give identifire by selecting your cell using storyboard Attribute inspector uniqueIdentifire to identifire property see image.
Also you can give some vertical space between cells by just to add this below code (Method only) inside customeCellClass.
- (void)setFrame:(CGRect)frame { // method to insert gap between table view cell
frame.origin.y += 6;
frame.size.height -= 2 * 6;
[super setFrame:frame];
}
You can not really change the frame of the inbuilt subviews of uitableviewcell like imageview, accessoryview. But if you create a custom tableviewcell class(even if you do not add any other subelement to it), you can change the frame of the inbuilt imageview by overriding the layoutSubviews method inside the UITableViewCell. I have tried it and it works.
#import "TableViewCell.h"
#implementation TableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) layoutSubviews{
[super layoutSubviews];
CGRect frame = self.imageView.frame;
frame.origin.x = 0;
self.imageView.frame = frame;
}
#end

Add subview to a custom class

I have a UITextField that I want to create a custom class on. So I created a file with a subclass of UITextField. Next, in the custom class, I want to implement a tableView. Kind of like a auto-complete textField.
I started creating it, and added the tableView like this:
[self addSubview:self.tableView];
When I run the app, the tableView is in the textField, so I can only see part of the tableView. How can I add it as a subview so I can see the full tableView?
This is what you are looking for
https://github.com/gaurvw/MPGTextField
This uitextfield subclass does what you want - it's builed for 'search' feature.
If you still want to use your own,
add tableview not to uitextfield itself, but like
[[self superview] addSubview:tableViewController.tableView];
EDIT:
you can set frame as:
CGRect frameForPresentation = [self frame];
frameForPresentation.origin.y += self.frame.size.height;
frameForPresentation.size.height = 200;
[tableViewController.tableView setFrame:frameForPresentation];
The way to add subview to uitextfield is to overload layoutSubviews method and init your tableview there:
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.tableview.superview)
{
[self setupView];
}
}
This will add the tableView as the subView of the textField.
self.tableView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), YOUR_TABLE_HEIGHT);
[self addSubview:self.tableView];
self.clipsToBounds = NO;
However, a better way is to make the tableView as the textField's superView's subView, that is, the textField and the tableView should be siblings.

Added UITextField as a subview of UITableViewCell working fine in IOS 6 but in IOS 7 it is not working?

In my app i have a login form, where user enter one field password. so i added a subveiw of UITextField in UITableViewCell during function call of cellForRowAtIndexPath.
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"loginViewCell"];
UITextField *editableTextField = nil;
// Done Some settings of editableTextField
// Adding Subview
[cell addSubview:editableTextField];
When user press login button i called a selector function name login
UITableViewCell *cell = [loginTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
// Then i travesed in the subviews of cell and from UITextField subview i extract password which was entered by user.
for (UIView *view in cell.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
usernameField = (UITextField *)view;
break;
}
}
This is working fine till IOS 7
After searching on net i trace the problem that in IOS 7 Apple changes the view hierarchy of UITableViewCell and now there is additional class inserted UITableViewCellScrollView.
I debug my code in my selector function login it is getting the same cell in which i added the subview i print the name of cell.subview is is showing UITableViewCellScrollView previously it was showing UITableViewCellcontentView (before ios7)
How can I extract the password from subeview of UITableViewCell?
The cellForRowAtIndexPath: method is not the right place to add subviews but rather to manipulate them.
You should really create a subclass of UITableViewCell having a #property UITextField which is added to the cell within the initWithStyle:reuseIdentifier: method.
[self.contentView addSubview:self.passwordField];
Then you can access this particular UITextField with [cell passwordField] or whatever you want to call it.
The easiest way would be to subclass your UITableView cell and add a class property that is your UITextField. Then you could just call cell.textField.text instead of searching through the view hierarchy.
The second way would be to search recursively through the entire view hierarchy, not just a single layer of subviews. (and you should be searching the cell.contentView anyway, bad things happen when you add views as subview's of the cell directly.)
EDIT adding code for searching recursively through view hierarchy. I do not recommend this method, I recommend subclassing UITableViewCell (it will make your like so much easier), but here you go.
You would call a function like
UIView *yourFoundSubview = [self findTextFieldInCell:cell];
And that function would be defined:
-(UIView*)findTextFieldInCell:(UIView*)input
{
if([input isKindOfClass:[UITextField class]])
{
return input;
}
UIView *foundview;
for(UIView *view in input.subviews)
{
foundview = [self findTextFieldInCell:view];
if(foundview)
{
return foundview;
}
}
return nil;
}
I believe you need to get the subviews of the UITableViewCellScrollView.
Instead of adding the textfield to the cell, add the UITextField to cell.contentView, and look for the textField in the subviews of the cell.contextView.
I think what would really be best in the long run for your solution though, would be to create a custom UITableViewCell, and add the textField in there. You could directly access your textfield that way without having to loop through the subviews of the cell.contentView.
You could do 2 things:
If you have only have 1 textfield for you entire viewcontroller, you define a property that holds a reference to your passwordTextField
#property(strong, nonatomic) UITextField *passwordTextfield;
Or, if you have a textfield for each tableviewCell, you could define a collection of UITextFields
#property(strong, nonatomic) NSMutableArray *textFields;
then you can reference your passwordTextField with:
UITextField *passwordField = self.textFields[PASSWORD_ROW];

Programmatically scroll UIScrollView using subview element

I would like to know how to access a UIScrollView using a subview UILabel.
I have tried to access the UIScrollView using .superview; however I am now receiving an error
No visible #interface for 'UIView' declares the selector 'scrollRectToVisible:animated:'
The code I am using looks like this
- (void) SymbolButtonPressed:(NSString *)selectedString {
UILabel *label = (UILabel *)[self.view viewWithTag:currentlySelectedTag];
// perform scrolling here, figure out what view your uilable is in.
float newPosition = label.superview.contentOffset.x+label.frame.size.width;
CGRect toVisible = CGRectMake(newPosition, 0, label.superview.frame.size.width, label.superview.frame.size.height);
[label.superview scrollRectToVisible:toVisible animated:YES];
}
The superview of a UILabel is of type UIView and so does not respond to the method you are trying to call. You can cast the superview as a UIScrollView so that Xcode can see the methods and properties you are trying to access. You should also check if the superview responds to the method.
if([label.superview respondsToSelector:#selector(scrollRectToVisible:animated:)]) {
[(UIScrollView *)label.superview scrollRectToVisible:toVisible animated:YES];
}
Given your sample code you will also need to cast the superview to get contentOffset
float newPosition = ((UIScrollView *)label.superview).contentOffset.x+label.frame.size.width;

How to create a UITableCell that has multiple subtitles

I came across this design image online and I am really puzzled on how could I make a UITableCell that has multiple subtitles and allows me to customised them in the way shown by the picture.
My understanding is that one can only use 1 subtitle per cell.
Is there a way to create a UITable cell that looks like that? How would you go on to make those 4 subtitles under the cell title???
You could do that easily by having a custom layout for the UITableViewCell. This video should help you in doing this:
http://www.youtube.com/watch?v=d_kO-J3DYvc
Basically you will need to design the UI of the cell in storyboard/NIB file and add multiple labels to your table cell there. Sub-class UITableViewCell and link it to the designed UITableViewCell in storyboard/NIB. Addd IBOulets in that class for the labels and link your labels from the UI to these IBOutlets.
From the image provided, it looks like the prototype UITableViewCell contains one UIImageView and 5 UILabels. Assuming you are using IB or storyboard to create the table view cell, set the 'Table View Style' to Custom, than drag a UIImageView and 5 UILabels onto the prototype cell. For each of the UILabels, adjust their position, font and font size as desired. You may also need to adjust the height of the cell.
Hey I have created a Sample Project regarding Custom Cell check this github link that I have created. I have used storyboard.
here is the screenshot of the sample app
Just subclass UITableViewCell, and add in multiple UILabel's. Then override the layoutSubviews method to position those labels. Then in the cellForRow, make sure you instantiate your subclassed UITableViewCell. I don't have time to check this, but the subclass would look something like:
#interface CustomCell : UITableViewCell {
UILabel *myCustomLabel1;
}
#end
#implementation CustomCell
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
myCustomLabel1 = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:myCustomLabel1];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
float margin = 5;
[myCustomLabel1 setFrame:CGRectMake(self.bounds.origin.x+margin, self.bounds.origin.y, self.bounds.size.width - (2*margin), self.bounds.size.height)];
}
#end

Resources