TableViewCell view disappear - ios

I have UITextField at my UItableviewCell , when I click done button on keyboard , my Cell views is removed from Superview
override func layoutSubviews() {
super.layoutSubviews()
self.selectionStyle = .none
line.backgroundColor = .custom_gray()
line.snp.makeConstraints { (cons) in
cons.bottom.left.right.equalTo(phone).inset(0)
cons.height.equalTo(0.5)
}
stack.snp.makeConstraints { (cons) in
cons.left.right.equalTo(self).inset(25)
cons.centerY.equalTo(self)
cons.height.equalTo(230)
cons.bottom.equalTo(self).inset(15)
}
stack.dropShadow()
stack.layoutIfNeeded()
}

I solve this problem By adding my View Into TableviewCell content view
self.contentView.addSubview(stack)
phone.delegate = self
password.delegate = self
stack.snp.makeConstraints { (cons) in
cons.left.right.equalTo(self.contentView).inset(25)
cons.centerY.equalTo(self.contentView)
cons.height.equalTo(230)
cons.bottom.equalTo(self.contentView).inset(15)
}
self.contentView.backgroundColor = .white
and I return this view from tableviewHeaderContent
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let head = tableView.dequeueReusableCell(withIdentifier: headerid) as! ProfileLoginTableViewCell
return head.contentView
}

Related

Two tableviews in one view controller, one table view not showing up

I have two TableViews in one ViewController and only one table is able to populate. I have created outlets for both tables in my view controller. Here is my viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
pickerTableView.delegate = self
pickerTableView.dataSource = self
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
pickerTableView.reloadData()
tableView.reloadData()
}
It seems that the only table that is read is self.tableView and the pickerTableView is never read in the protocols for UITableView
extension MealViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView == self.tableView {
let nameHeaderView = NameHeaderView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 40))
nameHeaderView.delegate = self
nameHeaderView.sectionIndex = section
nameHeaderView.nameButton.setTitle(Data.userModels[section].name, for: .normal)
return nameHeaderView
} else {
return nil
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return Data.userModels.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
if Data.userModels[section].isExpandable {
return Data.userModels[section].itemModels.count
} else {
return 0
}
} else if tableView == pickerTableView {
return Data.userModels.count
}
else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCells", for: indexPath) as! MealTableViewCell
cell.setup(itemModel: Data.userModels[indexPath.section].itemModels[indexPath.row])
return cell
} else if tableView == pickerTableView {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "pickerCells", for: indexPath) as! NamePickerTableViewCell
cell2.setup(userModel: Data.userModels[indexPath.row])
print(Data.userModels)
return cell2
}
else {
return UITableViewCell()
}
}
}
I have also applied constraints on each tableView so they both appear on the screen. When I run the app, the data for self.tableView is shown however the pickerTableView appears empty. self.tableView requires sections and pickerTableView does not require sections, could this be an issue? Also there is data in the Data.userModels array.
And just for reference, here is my UITableViewCell class:
class NamePickerTableViewCell: UITableViewCell {
#IBOutlet weak var namePickerLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setup(userModel: UserModel) {
namePickerLabel.text = userModel.name
}
}
Thanks for the help!
If you do not need any section in pickerTableView, then also it has to have at least one section.
So return 1 in method numberOfSections when tableview == self. pickerTableView.
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return Data.userModels.count
} else {
return 1
}
}

Cosmic Mind - How to resize view when TabsController is implemented

I am having a problem on inserting a title to this screen.
How can I resize this view?
Here's my code where the TabsController is implemented:
class DashboardViewController: TabsController {
let screenSize = UIScreen.main.bounds
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare() {
super.prepare()
tabBar.lineColor = UIColor.CustomColor.blue
tabBar.setTabItemsColor(UIColor.CustomColor.grey, for: .normal)
tabBar.setTabItemsColor(UIColor.CustomColor.blue, for: .selected)
tabBarAlignment = .top
tabBar.tabBarStyle = .auto
tabBar.dividerColor = nil
tabBar.lineHeight = 2.0
tabBar.lineAlignment = .bottom
tabBar.backgroundColor = .white
}
}
Here's my option one code (and the option two code is the same):
class TeamProjectViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
prepareTabItem()
setupTableView()
}
fileprivate func setupTableView() {
tableView.backgroundColor = .white
tableView.allowsSelection = false
tableView.separatorColor = UIColor.CustomColor.lightGrey
tableView.register(UINib(nibName: "ProjectTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ProjectTableViewCell
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
}
extension TeamProjectViewController {
fileprivate func prepareTabItem() {
tabItem.title = "Option 1"
}
}
And here's what's happening to my tabs:
Thank you!
In your code you do not have anywhere where you are actually setting a view over the TabsController, where a title could exist. You would need to wrap the TabsController in order to accomplish this. One way, is use a ToolbarController:
let toolbarController = ToolbarController(rootViewController: myTabsController)
toolbarController.toolbar.title = "my title"
This is one way of going about your issue.
If you want just 20 pixels to move the tabs below status bar you can use StatusBarController with displayStyle = .partial. That's how I workaround.
let tabsController = TabsController(viewControllers: [
MyViewController1(),
MyViewController2()
])
let statusBarController = StatusBarController(rootViewController: tabsController)
statusBarController .displayStyle = .partial
// add statusBarController to hierarchy

how to subclass UITableView to use in other viewControllers

I'm looking to create a subclass for a tableview that I would like to use throughout out all my app. My problem is when I set the fields variable the tableview is still empty. I'm assuming that the fields variable is not setting right when it is set in the new ViewController. Thank you for the help in advance.
MySubClass
import UIKit
import LGButton
class RquestTableViewController: UITableViewController {
var fields:[UIView]
override func viewDidLoad() {
super.viewDidLoad()
viewSetup()
}
func viewSetup(){
tableView.register(CustomFormCell.self, forCellReuseIdentifier: labelReuseCellIdentifier)
self.tableView.tableFooterView = UIView.init(frame: CGRect.zero)
}
}
//MARK: Delegate
extension RquestTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return fields.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: self.labelReuseCellIdentifier, for: indexPath) as! CustomFormCell
let itemView = fields[indexPath.row]
cell.viewPassed = itemView
cell.backgroundColor = .clear
if let _ = itemView as? UILabel {
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
return cell
}
if let _ = itemView as? UIButton {
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
return cell
}
if let _ = itemView as? LGButton {
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
return cell
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40.0
}
}
MyNewViewController
class NewRquestViewController: RquestTableViewController {
lazy var password:JVFloatLabeledTextField = {
let v = JVFloatLabeledTextField()
v.placeholder = "Password"
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
fields = [password]
}
}
Have you verified that fields really isn't being set properly? I see no reason why it wouldn't be. But I do see another problem: in addition to overriding numberOfSections(in:) and tableView(_,cellForRowAt:), you have to override tableView(_,numberOfRowsInSection:). What you have now is numberOfSections(in:) returning the number of fields, but tableView(_,cellForRowAt:) is expecting to populate the rows with the fields. That means that tableView(_,cellForRowAt:) will be called only when section is 0, so at most you'd see only one field. Instead, change numberOfSections(in:) to return 1, and implement tableView(_,numberOfRowsInSection:) to return fields.count.

How do you change the colour of a section title in a tableview?

Here is what I have at the moment.
How do I refer to this so that I can change the text colour to match my index list? The sectionForSectionIndexTitle worked well for adding in the correct section title but how exactly does one access the title element?
Or is it impossible and I need to redraw the view and add it with viewForHeaderInSection?
you can use the one of UITableViewDelegate's method
swift3 and above
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.contentView.backgroundColor = .white
headerView.backgroundView?.backgroundColor = .black
headerView.textLabel?.textColor = .red
}
}
objective C
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView * headerView = (UITableViewHeaderFooterView *) view;
headerView.textLabel.textColor = [UIColor RedColor];
}
}
for Reference I taken the model answer from here
One liner solution (using optional chaining):
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
(view as? UITableViewHeaderFooterView)?.textLabel?.textColor = UIColor.red
}
Custom Title:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let title = UILabel()
title.font = UIFont(name: "SFUIDisplay-Light", size: 13)!
title.textColor = UIColor.redColor()
let header = view as! UITableViewHeaderFooterView
header.textLabel!.font=title.font
header.textLabel!.textColor=title.textColor
header.contentView.backgroundColor = UIColor.whiteColor()
}
Swift Solution
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.red
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
I would use the Appearance() proxy class. I usually add them in a function in AppDelegate and call them didFinishLaunching.
private func setupApperances() {
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).textColor = .red
}
You can make your own section title (header/footer) view, and it is easy.
class BlackTableViewHeaderFooterView : UITableViewHeaderFooterView {
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .black
textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
textLabel?.numberOfLines = 0
textLabel?.textColor = .white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class TableViewController : UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(BlackTableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "\(BlackTableViewHeaderFooterView.self)")
// do other setup
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "\(BlackTableViewHeaderFooterView.self)")
header.textLabel?.text = "" // set your header title
return header
}
}

Two UITableView in single UIViewcontroller not working properly

I want to implement following functionality in app show pict
But i have following problem show another pict
my code as follow
// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if str == "Loading"{
return 0
}else if tableView == tbl2{
return arrSub.count
}else{
return self.displayData.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:customCellInvitation = self.tableView.dequeueReusableCellWithIdentifier("cell")as! customCellInvitation
if tableView == tbl2{
//Code for the load secind table
cell.lblUserName.text = self.arrSub.objectAtIndex(indexPath.row).valueForKey("username") as?String
cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Normal))
return cell
}else{
//Code for the load first table
cell.lblUserName.text = self.displayData.objectAtIndex(indexPath.row).valueForKey("username") as?String
cell.btnAdd.setImage(UIImage(named: "add.png"), forState:(UIControlState.Normal))
cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Selected))
cell.btnAdd.addTarget(self, action: "addData:", forControlEvents: .TouchUpInside)
cell.btnAdd.tag = indexPath.row
}
return cell
}
// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44
}
//function call when user click Plus button
func addData(sender: UIButton!) {
arrSub .addObject(self.displayData .objectAtIndex(sender.tag))
var button:UIButton = sender.viewWithTag(sender.tag) as! UIButton
button.selected=true
button.userInteractionEnabled = false
NSLog("%#", arrSub)
[tbl2 .reloadData()]
}
I would suggest you to move your tableView Datasource and Delegate to separate classes. This is not a good practise at all. You will certainly mess up with your code.
What you are doing make code complexity, you can add you own custom class for tableView and can maintain it it all delegate datasource methods.Add that tableview in current class and with giving frame to it. By this you can add as many number of tableview to a single class and no need to worry about data handling.
Put a frame to this table view, then you can add two frame to a view controller. So add this frame to your view controller, you should adjust the frame width and hight in order to show two tables.
class ViewController: UIViewController {
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .None
tableView.frame = CGRectMake(20, (self.view.frame.size.height - 54 * 5) / 2.0, (self.view.frame.size.width - 25 * 5), 54 * 5)
tableView.autoresizingMask = .FlexibleTopMargin | .FlexibleBottomMargin | .FlexibleWidth
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.opaque = false
tableView.backgroundColor = UIColor.clearColor()
tableView.backgroundView = nil
tableView.bounces = false
tableView.showsVerticalScrollIndicator = true
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clearColor()
view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// MARK : TableViewDataSource & Delegate Methods
extension LeftMenuViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 54
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let titles: [String] = ["Home", "Features", "Pricing", "Help", "About Us", "Contact Us"] // put your titles
let images: [String] = ["IconHome", "IconCalendar", "IconProfile", "IconSettings", "IconEmpty", "IconEmpty"] // add images if you want
cell.backgroundColor = UIColor.clearColor() // optional
cell.textLabel?.font = UIFont(name: "HelveticaNeue", size: 21)
cell.textLabel?.textColor = UIColor.whiteColor()
cell.textLabel?.text = titles[indexPath.row]
cell.selectionStyle = .None
cell.imageView?.image = UIImage(named: images[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
switch indexPath.row {
case 0:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("TabBar") as! UIViewController
sideMenuViewController?.contentViewController = viewController
sideMenuViewController?.hideMenuViewController()
break // show table navigation view controller
case 1:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("TabBar") as! UIViewController
sideMenuViewController?.contentViewController = viewController
sideMenuViewController?.hideMenuViewController()
break // show table navigation view controller
default:
break
}
}
}
// MARK: UITextFieldDelegate Methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:customCellInvitation = self.tableView.dequeueReusableCellWithIdentifier("cell")as! customCellInvitation
if tableView == tbl2{
cell.lblUserName.text = self.arrSub.objectAtIndex(indexPath.row).valueForKey("username") as?String
if str = "yes"{
cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Normal))
}else{
cell.btnAdd.setImage(UIImage(named: "NO.png"), forState:(UIControlState.Normal))
}
return cell
}else{
cell.lblUserName.text = self.displayData.objectAtIndex(indexPath.row).valueForKey("username") as?String
if str = "yes"{
cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Normal))
}else{
cell.btnAdd.setImage(UIImage(named: "NO.png"), forState:(UIControlState.Normal))
}
}
return cell
}

Resources