How to remove the bottom line of UITableView in iOS - ios

Whenever I create a tableView,there will be a line at the bottom of the last cell,is there a method to remove it?I don't use the Xib or Storyboard,so please show me the code.Thank you very much!
Sorry I didn't describe the situation clearly,here is the Screenshot,I have two sections in this tableview,and there is a line at the bottom of the last cell

The solution is really easy if you just take a custom cell.
Firstly drag and drop a cell in your TableView and then select the TableView to make it's separator as None like-
Then Assign the UITableViewCell's class top the custom one.
In the TableViewCell in storyboard, take your required Objects like Lables, images or whatever you need. Now, make sure you take a UIView of 1 px at the bottom of the cell. Connect the outlets and add the required constrains.
Your Custom Class may look like -
CustomTableViewCell.h file-
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell
#property(nonatomic, strong) IBOutlet UILabel *customTextLabel;
#property(nonatomic, strong) IBOutlet UIView *separatorView;
#end
Now in the View Controller, just show or hide the separator at the bottom row of your section.
So, your Datasource methods may look like-
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return 2;
}
else{
return 1;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"sampleCell"];
if(indexPath.section == 0 && indexPath.row == 1){
cell.separatorView.hidden = YES;
}
else if (indexPath.section == 1 && indexPath.row == 0){
cell.separatorView.hidden = YES;
}
else{
cell.separatorView.hidden = NO;
}
cell.customTextLabel.text = #"Test"; //put whatever you want
return cell;
}
You should get a outlook like-
Hope this helps.

For removing last line i.e last separator of tableview, you have to use below code in UIViewDidload
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
OR if you want to remove particular cell separator use inside cellForRowAtIndexPath method
tableView.separatorStyle = UITableViewCellSeparatorStyleDefault;
if (indexPath.section == 1 && indexPath.row == 1)
{
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

To hide the line below the cell use the following:
// set properties tableView
tableView.separatorStyle = .none

You can design your own custom footer for UITableView
But i don't think by default tableView show any footer.
As Your question is unclear and you haven't provide any screenshot or anything else.
Create your custom view with your desired colour contents and then just assign it to your tableView.
self.tableView.tableFooterView = customFooterView;
Or Maybe you want to hide/remove the tableView LineSeparator
There are two most easy approaches to do this.
Either assign a clear colour to separator or assign none to separator.
self.tableView.separatorStyle = UITableViewSeparatorStyleNone;
or
self.tableView.separatorColor = [UIColor clearColor];

Related

Objective c different uilabel in a row of a table cell

I am trying to create a table that in each row, it contains some uilabels. The number of the uilabel is based on my array, if my array has one string then it would only be one label, if my array has 2 strings then the row will have two labels.
First Row:
Second Row:
Here's my code to put in the strings into the uilabels that are already there. How to make the cell to auto generate somemore uilabels if I have more strings?
- (void)updateCell:(NSString *)text1 label2:(NSString *)text2 label3:(NSString *)text3{
self.testLabel1.text = text1;
self.testLabel2.text = text2;
self.testLabel3.text = text3;
}
- (void)createLabel{
//create uilabels based on the size of array?
}
If this is not workable, what might be some alternative ways? Any advice is much appreciated.
This should all be done in cellForRowAtIndexPath, you should basically use your array for that row index and then create the uilabels programmatically, here is some code, it won't work but the logic should be okay
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *stringLabels = [self.labels objectAtIndex:indexPath.row]; // im not sure how you are getting the array
for (NSString *str in stringLabels) {
UILabel *newLabel = [[UILabel alloc]init];
newlabel.SetFrame = CGrectMake(Set to your desired layout);
newlabel.text = str;
// uilabel formatting
[cell addSubview:newLabel]
}
}
This approach is very flexible as it allows you to have any number of labels in every cell.
Try this
You need to create different cell as per your condition and configure CellForAtIndexPath as below :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *strFirstIndentifier = #"FirstIndentifier"; // set this idenfier to UITableViewCell in StroryBoard for all
NSString *strSecondIndentifier = #"SecondIndentifier";
NSString *strThirdIndentifier = #"ThirdIndentifier";
UITableViewCell *adjustcell ;
if (arrUnits.count == 1) {
adjustcell = [tableView dequeueReusableCellWithIdentifier:strFirstIndentifier];
//configure your lables here
}
else if (arrUnits.count == 2) {
adjustcell = [tableView dequeueReusableCellWithIdentifier:strSecondIndentifier];
//configure your lables here
}
else if (arrUnits.count == 3) {
adjustcell = [tableView dequeueReusableCellWithIdentifier:strThirdIndentifier];
//configure your lables here
}
return adjustcell;
}
I would recommend using a UICollectionView with the data source of the UICollectionView be an array that will hold the number of strings in that particular row. Each cell of the UICollectionView will hold one label. I personally find this way easier to manage.

xcode and storyboard: how build an user profile view

how can i build an user profile view like this (Periscope but is similar to many other apps).
It's a tableviewcontroller? If it is, how can i put the image of the user with background (it's in the first cell or above the tableview?)
I'd build it this way:
UIViewController with UITableView + custom UIView on top.
If you want to use already implemented libraries, check this out :
MGSpotyViewController
jcbannerView
Facade
They have pretty similar logic that's described in your question.
It is a custom tableviewcontroller. Everything is a cell but it is hard to create only using storyboard. you create a dynamic table view and ad 3 prototype cell for it (1: Blue cell, 2:Grey empty cell, 3: Option Cell). And create a controller and manage the cell with it like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row== 0)
{
HeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:#"headerCell" forIndexPath:indexPath];
cell.name = "foo";
....
}
else if(indexpath.row ==1 || indexpath.row ==3)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"blankCell" forIndexPath:indexPath];
[cell setBackgroundColor:[UIColor greyColor]];
}else{
....
}
}
Looks like above is a header view, and below a table view.
If the above part scrolls off it is the table view's tableHeaderView. In this case you can use a UITableViewController.
Otherwise you will have to use a UIViewController with a UIView as the header and a UITableView below it, and you have to declare the table view delegate and datasource yourself.
You can achieved same UI with help of bringing UITableView and add UIView with blue background as tableHeaderView like as below.
UIView *headerView = [[UIView alloc] init...];
tableView.tableHeaderView = headerView;

Custom cell image deforms when scrolling

I have a custom cell for my UITableView ; that custom cell comes from a class that extends UITableViewCell.
There is only a .xib where i crated the actual cell, and the links to these items in the .h.
I'm not using the .m, it's only the autogenerated awakeFromNib and setSelected:selected:animated in there.
When i create my cell in the cellForRow method, it's all fine, it appears correctly on the screen.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CustomCellTableViewCell";
CustomCellTableViewCell *cell = [self.tbUpcomingEvents dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
tmp = [_eventList objectAtIndex:indexPath.row];
cell.lbDescription.text = tmp.description;
cell.lbTitle.text = tmp.title;
cell.imageView.layer.cornerRadius = 20;
cell.imageView.clipsToBounds = YES;
cell.layer.masksToBounds = YES;
cell.imageView.image = [UIImage imageNamed:tmp.type.typeImage];
cell.lbTimeStart.text = [[[timeFormatter stringFromDate:tmp.startTime] stringByAppendingString:#" - "]stringByAppendingString:[dateFormatter stringFromDate:tmp.startDate]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
My issue is the following : when I start scrolling the tableview, the NEW cells are messed up, specifically the image. It simply gets bigger/wider and (obviously) deformed. I don't really know what caused it or how to fix it.
The cell is associated with the reuse identifier from the cellForRow method and the custom class in the .xib file. The UITableView has nothing in particular (and has no other link than the storyboard -> .h, delegate, datasource)
Any clue?
I found my issue. In this specific case, my imageView in my customCell.h was called "imageView". Which is a reserved named and, i guess, created conflicts at (some?) points in the creation of the cells.
I don't know the why's, but i know what fixed the issue. Now i've simply put another property name for my image and i'm golden ;)
Try to replace your code:
cell.lbDescription.text = tmp.description;
cell.lbTitle.text = tmp.title;
cell.imageView.image = [UIImage imageNamed:tmp.type.typeImage];
cell.lbTimeStart.text = [[[timeFormatter stringFromDate:tmp.startTime] stringByAppendingString:#" - "]stringByAppendingString:[dateFormatter stringFromDate:tmp.startDate]];
from cellForRowAtIndexPath to willDisplayCell method.
The idea is to make UITableViewCells templates in tableView:cellForRowAtIndexPath:, but to fill cells with particular values in tableView:willDisplayCellForRowAtIndexPath:.
uncheck autoresize subviews in interface builder

Can I disable the UITableView selection of the individual cell?

When ever you tap on a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.
Implement tableView:willSelectRowAtIndexPath: delegate method of UITableView and return nil.Returning nil from this method tells the table view to not select the row consequently tableView:didSelectRowAtIndexPath: method will not get called.But doing this does not prevent highlighting the row.To disable highlighting effect on row set cell selection style toUITableViewCellSelectionStyleNone as explained in the above answers.
Just set :
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
If you want stop highlighting use the code say: your desired value to be 19
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row == '19')
cell.selectionStyle = UITableViewCellSelectionStyleNone;
else
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
But, if you want to control the selection of cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexpath.row == '19')
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
for Swift 3 :
cell.selectionStyle = UITableViewCellSelectionStyle.none
The accepted answer was not working for me. This problem driving me crazy for awhile because it would look like a "glitch" on any selection when the next view loaded..
My SOLUTION: I had to go to the .xib file of the actual TableViewCell (my custom cell that I built) select the cell and select the CellView. (Make sure you select the Cell, and NOT a subview within it. It will turn Blue like the picture shows). On the left side click "Selection" -> "None". This SHOULD be the exact same as doing it with code, but for some reason it just wasn't working...
This is for the case in which you have a custom cell, but I found this question when I was looking for the answer, so I thought I'd leave this here in case this helps others.
Go to your custom cell, and set selectionStyle to .none in the awakeFromNib method:
override func awakeFromNib() {
super.awakeFromNib()
// disable selection
selectionStyle = .none;
}
If you use this custom cell in multiple UITableViews, you only have to set it once in the custom cell instead of on every UITableView you use :)
for Swift 4
cell.selectionStyle = UITableViewCell.SelectionStyle.none
you could simply do this for OBJ C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;

Is this an example of an iOS UITableView?

I need to do something like this :
.
Is it a UITableView? Or how should I do this? Any idea is welcome.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 0) return cell1;
} else if (indexPath.section == 1) {
if (indexPath.row == 0) return cell2;
} else if (indexPath.section == 2) {
if (indexPath.row == 0) return cell3;
}
}
Most likely its a UITableView. May be its not (it can be a UIScrollView with views added like sections in a table view).
But if you want to create something like in the image, I'd suggest you to use UITableView with customized cells. Because implementing table view, in this case, is simpler as you have to just concern about the delegate and the dateSource of the table view, rather than worrying about aligning the views in order.
Use sectioned table view with the translucent, gray-bordered, black-background-view as the backgroundView of the cells. Add the labels and arrow as the subviews of cell. You can add arrow as the accessoryView but that would become vertically centered, but in the image the arrow is displayed slightly on top of the cell.
There should be only one cell in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
There can be any number of sections (3 here).
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3; // Can be any number
}
Yes, that's most likely a styled table view. See this guide or this nice tutorial.
why you worry about this one? Its simple man.... its nothing ,if you have hands over UITableView. Yeah, You must know about :
1: Set background image on view,exact as table size.
2: Set table view onto them and make background transparent.
3: Make customize table cell and add to table' row.
Thats it.....enjoy this work..its a creativity.
And you can do it guy....
I cannot tell exactly what is used from the picture, however I suggest using the UITableView especially if you have variable data coming from a structured object.
Some tutorials which I found very useful are the following:
Creating customized UITableViewCell from XIB
http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/
UIViewTable anti-patterns - A very good MVC pattern to build UIViewTable
http://www.slideshare.net/nullobject/uitableviewcontroller-antipatterns
And of course Apple's docs:
http:/developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
One important fact to remember is that you should always reuse cached table cells through the datasource method tableView:didSelectRowAtIndexPath:
Note: Since you want lots of transparencies there might be some performance issue especially on older devices.
Hope this helps and goodluck with your project.
There is not need to its scrollview or tableview... both are perform same..
Just change the Background Color of Tableview as clear color.....
check this code...
put this line to viewDidLoad :
[tableView setBackgroundColor:[UIColor clearColor]];
and
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
..... // Your label buttons etc........
......
......
.....
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setClipsToBounds:YES];
[[cell layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[cell layer] setCornerRadius:10];
}
You can do this using tableView in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//here place return your array contents
}
We can accomplish the similar as shown in image above by Table View and scroll view both.If you don't want to use table view then you can take a scroll view or a simple view and can add several buttons along with the image in background with different frames.It means button background will have the same image.then you can use different labels to show all the texts in different labels according to the requirement.
OR
you can use table view.For this you need to return number of sections 3(in this image) in the delegate method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.and then everything as the default tableview style.And then change the cell style to detailed.
yes this is a tableView with custom UITableViewCell.you can do that by creating a empty xib file and then add a UITableViewCell and do your customizing ...then in cellForRowAtIndexPath method use:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:edentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"yourCellName" owner:self options:nil]objectAtIndex:0];
this is from my application.
Yes, it is a groupedtableview with clearcolor as backgroundcolor. Cell background color also needs to be changed to clearcolor.
I suggest using div or list, don't use table, its very hard to control if the contents are too long.

Resources