質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

1084閲覧

swift tableviewの空白

Yuichan1210

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/04/28 14:25

編集2021/04/29 02:34

swift

1import Foundation 2import XLPagerTabStrip 3 4class SecondAsiaViewController:UIViewController,IndicatorInfoProvider,UITableViewDelegate, UITableViewDataSource{ 5 6 var itemInfo:IndicatorInfo! 7 var myTableView:UITableView? 8 var sectionTitle:[String] = ["ア行","カ行","サ行","タ行","ナ行","ハ行","マ行","ヤ行","ラ行","ワ行"] 9 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 self.view.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) 14 self.itemInfo = "アジア" 15 tableViewSetUp() 16 } 17 18 19 func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { 20 return itemInfo 21 } 22 23 func tableViewSetUp() { 24 myTableView = UITableView() 25 myTableView?.delegate = self 26 myTableView?.dataSource = self 27 myTableView?.frame = CGRect(x: 0, y: 0 , width: self.view.bounds.width, height: self.view.bounds.height / 2) 28 //myTableView?.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2) 29 myTableView?.frame.origin = .zero 30 myTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 31 myTableView?.sectionHeaderHeight = 30 32 myTableView?.separatorInset = UIEdgeInsets.zero 33 myTableView?.layer.borderColor = UIColor.red.cgColor 34 myTableView?.layer.borderWidth = 6 35 myTableView?.rowHeight = 55 36 self.view.addSubview(myTableView!) 37 38 } 39 40 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 41 switch section { 42 case 0: 43 return 3 44 case 1: 45 return 3 46 case 2: 47 return 5 48 default: 49 return 0 50 } 51 } 52 53 func numberOfSections(in tableView: UITableView) -> Int { 54 return sectionTitle.count 55 } 56 57 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 58 return sectionTitle[section] 59 } 60 61 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 62 let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) 63 switch indexPath.section { 64 case 0: 65 cell.textLabel?.text = "こんにちは" 66 case 1: 67 cell.textLabel?.text = "こんにちは" 68 case 2: 69 cell.textLabel?.text = "こんにちは" 70 default: 71 cell.textLabel?.text = "こんにちは" 72 } 73 cell.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) 74 cell.accessoryType = .disclosureIndicator 75 cell.textLabel?.numberOfLines = 0 76 return cell 77 } 78 79 func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 80 view.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1) 81 } 82 83 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 84 print("(indexPath.row)番目が選択されました!") 85 86 let vc = AsiaIndexContentViewController() 87 self.present(vc, animated: true, completion: nil) 88 } 89} 90

イメージ説明
イメージ説明

なぜtableviewを追加した時、画像のような白い空白ができるのでしょうか?そしてスクロールするとcellが白い空白の上を通過します。どうすれば解決するでしょうか?アドバイスお願いします。

swift

1class SecondViewController: ButtonBarPagerTabStripViewController,UICollectionViewDelegateFlowLayout { 2 3 lazy var collectionView:UICollectionView = { 4 () -> UICollectionView in 5 6 let cv = buttonBarView as UICollectionView 7 return cv 8 }() 9 10 lazy var scrollView:UIScrollView = { 11 () -> UIScrollView in 12 let sv = containerView as UIScrollView 13 return sv 14 }() 15 16 override func viewDidLoad() { 17 menuBarSetUp() 18 19 super.viewDidLoad() 20 basicSetUp() 21 22 self.view.addSubview(collectionView) 23 print(collectionView.frame.size.height) 24 self.view.addSubview(scrollView) 25 } 26 27 func basicSetUp(){ 28 self.view.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1) 29 self.navigationItem.title = "Test" 30 self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1) 31 self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white] 32 } 33 34 func menuBarSetUp() { 35 self.settings.style.buttonBarMinimumLineSpacing = 0 36 self.settings.style.buttonBarLeftContentInset = 0 37 self.settings.style.buttonBarRightContentInset = 0 38 self.settings.style.buttonBarBackgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1) 39 self.settings.style.selectedBarBackgroundColor = .black 40 self.settings.style.buttonBarItemsShouldFillAvailableWidth = false 41 self.settings.style.buttonBarItemBackgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1) 42 self.settings.style.buttonBarItemTitleColor = .black 43 44 self.changeCurrentIndexProgressive = {(oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool,animated: Bool) -> Void in 45 46 oldCell?.label.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6) 47 newCell?.label.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1) 48 49 50 } 51 } 52 53 override func viewWillLayoutSubviews() { 54 super.viewWillLayoutSubviews() 55 56 let topInset = self.view.safeAreaInsets.top 57 let width = self.view.bounds.width 58 let height = self.view.bounds.height 59 60 collectionView.frame = CGRect(x: 0, y: topInset, width: width, height: 56) 61 scrollView.frame = CGRect(x: 0, y: topInset + 56, width: width, height: height - 56) 62 } 63 64 override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { 65 66 let vc1 = SecondAsiaViewController() 67 let vc2 = SecondAfricaViewController() 68 let vc3 = SecondOceaniaViewController() 69 let vc4 = SecondEuropaViewController() 70 let vc5 = SecondNorthAmericaViewController() 71 let vc6 = SecondSouthAmericaViewController() 72 73 return [vc1,vc2,vc3,vc4,vc5,vc6] 74 } 75}

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/04/28 17:23

PagerTabStripViewController のサブクラスを storyboard で配置してますか? その際の containerView の制約はどうなってますか?
Yuichan1210

2021/04/28 23:31

override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() let topInset = self.view.safeAreaInsets.top let width = self.view.bounds.width let height = self.view.bounds.height collectionView.frame = CGRect(x: 0, y: topInset, width: width, height: 56) scrollView.frame = CGRect(x: 0, y: topInset + 56, width: width, height: height - 56) } ストーリーボードでcontainerViewは配置しておらずこのようにコードで位置を決めています。。
hoshi-takanori

2021/04/29 00:29

うーん、再現しませんね。scrollView の作り方が違うのかな…。あと、UITabBarController の中に UINavigationController がいて、その中に PagerTabStripViewController のサブクラスがある感じでしょうか。
Yuichan1210

2021/04/29 00:31

上記にscrollViewのところのコード載せました。。よろしくお願いします。
tomato879241

2021/04/29 02:24

自分だったらInterface Builderで考えられる背景の色を変えて、どの色が白からそれに変わるかで、問題の部分を判断しますけど。
Yuichan1210

2021/04/29 02:35

tableviewの色を黄色にしてcellを透明にしたtableview自体が黄色になりました。でもなぜか青のsectionの位置がおかしなところから始まってしまいます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問