The problem I am facing ishat i am unable to update the variable lin. I have tried multipleimes a f:) . To get to the relevode quick 2nd line in the code is where the variable is identified and then if u scroll to bottom of the code that is where the variable is suppose to change but does not
let menable = UITabletgView()
let arfSorces:[String] = ["C"]
public func runMenu(){
if let window = UIApplication.sharedApplication().keyWindow{
dimming.frame = window.frame
dimming.backgroundColor = UIColor(white: 0.9, alpha: 0.5087)
dimming.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector("eefefef")))
let height: CGFloat = 100000
let y = window.frame.height - height + height
menuTable.frame = CGRect(x: 10, y: window.frame.height, width: window.frame.height, height: height)
window.addSubview(dimming)
window.addSubview(mee)
eView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("c", forIndexPath: indexPath)as UITableViewCell
e.textLabel?.text = ars[indexPath.item]
return e
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50000
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
func RunLink(){
if let v = c{
98bdfe8daf6543d92"
Inside the if statement you declare a brand-new variable link1 that only exists inside that if statement. Change
if arrayOfSorces[indexPath.item] == arrayOfSorces[0]{
var link1 = "https://newsapi.org/v2/everything?sources=bbc-news&apiKey=a9ea5ee627044bd98bdfe8daf6543d92"
RunLink()
}
To
if arrayOfSorces[indexPath.item] == arrayOfSorces[0]{
link1 = "https://newsapi.org/v2/everything?sources=bbc-news&apiKey=a9ea5ee627044bd98bdfe8daf6543d92"
RunLink()
}
(Get rid of the var. That creates a new variable rather than changing the value that's in the existing variable.)
Related
I am trying to constraint elements (UIView or UITextfield) in a TableViewCell. If i am using a frame like
UITextField(frame: CGRect(x: 150, y: 0, width: 500, height: 110))
everything works fine.
But i am trying to use autoresize and somehow my UITextfield added as SubView in my Cell don't have the same ancestor. The error message is: "they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies?"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var Testtext: UITextField{
let test = UITextField(frame: CGRect(x: 150, y: 0, width: 500, height: 110))
test.textColor = .red
test.font = .boldSystemFont(ofSize: 22)
test.text = "Hello World"
return test
}
var cell:UITableViewCell {
let createdCell = UITableViewCell()
createdCell.contentView.addSubview(Testtext)
// Testtext.translatesAutoresizingMaskIntoConstraints = false
// NSLayoutConstraint.activate([
// Testtext.topAnchor.constraint(equalTo: createdCell.topAnchor),
// Testtext.bottomAnchor.constraint(equalTo: createdCell.bottomAnchor),
// Testtext.trailingAnchor.constraint(equalTo: createdCell.trailingAnchor),
// Testtext.leadingAnchor.constraint(equalTo: createdCell.leadingAnchor)
// ])
return createdCell
}
BTW: I don't want to create a Cellclass, because i need to adjust the number of Textfields and UIViews depending on the input arrayscount.
You need
var cell:UITableViewCell {
let createdCell = UITableViewCell()
createdCell.contentView.addSubview(Testtext)
Testtext.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
Testtext.topAnchor.constraint(equalTo: createdCell.contentView.topAnchor),
Testtext.bottomAnchor.constraint(equalTo: createdCell.contentView.bottomAnchor),
Testtext.trailingAnchor.constraint(equalTo: createdCell.contentView.trailingAnchor),
Testtext.leadingAnchor.constraint(equalTo: createdCell.contentView.leadingAnchor)
])
return createdCell
}
but this isn't the correct way you need to dequeueResuableCell inside cellForRowAt to generate the cells instead of creating vars
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
return cell
}
I want to create a footer which is dynamic to the height of the tableview cells.
My initial situation looks like:
If I click on a row, it change the height of this cell to 194 (44 before)
It don't show all rows.
If I get the footer -150 it looks like:
And if I close all cells it looks like and the 150 which I get to footer with -150 are white here:
My code:
var selectedCellIndexPath: Int?
let selectedCellHeight: CGFloat = 194.0
let unselectedCellHeight: CGFloat = 44.0
var cellsHeight = [44, 44, 44]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.beginUpdates()
if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath.row {
selectedCellIndexPath = nil
}
else {
selectedCellIndexPath = indexPath.row
}
if selectedCellIndexPath != nil {
tableView.scrollToRow(at: indexPath, at: .none, animated: true)
}
tableView.endUpdates()
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let rowHeight:CGFloat = 44
var rowsHeight:CGFloat = 3 * rowHeight
/*for i in 0..<cellsHeight.count {
rowsHeight += CGFloat(cellsHeight[i])
}*/
let topHeight = UIApplication.shared.statusBarFrame.height + self.navigationController!.navigationBar.frame.height
let viewHeight = self.view.frame.height
let headerHeight:CGFloat = 30
let height = viewHeight - topHeight - rowsHeight - headerHeight //- 150
return CGFloat(height)
}
Only one row will have the height 194 and the other 44. Any ideas how to solve the problem?
Thx
Edit:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dateFormatter = DateFormatter()
//dateFormatter.dateStyle = DateFormatter.Style.short
dateFormatter.dateFormat = "dd.MM.yyyy HH:mm"
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "workoutDateCell", for: indexPath) as! WorkoutDateTableViewCell
cell.typeLabel.text = "Beginn"
cell.dateDatepicker.date = Date()
cell.dateDatepicker.tag = indexPath.row
cell.dateLabel.text = dateFormatter.string(from: cell.dateDatepicker.date)
cell.selectionStyle = .none
return cell
}
else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "workoutDateCell", for: indexPath) as! WorkoutDateTableViewCell
cell.typeLabel.text = "Ende"
cell.dateDatepicker.date = Date()
cell.dateDatepicker.tag = indexPath.row
cell.dateLabel.text = dateFormatter.string(from: cell.dateDatepicker.date)
cell.selectionStyle = .none
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "workoutSportsCell", for: indexPath) as! WorkoutSportsTableViewCell
cell.sportsLabel.text = "Sportart"
cell.sportstypeLabel.text = workoutSports[coreData.getSportsIndex()]
cell.sportsPicker.delegate = self
cell.sportsPicker.dataSource = self
cell.sportsPicker.selectRow(coreData.getSportsIndex(), inComponent: 0, animated: false)
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if selectedCellIndexPath == indexPath.row {
return selectedCellHeight
}
return unselectedCellHeight
}
To archive this you can use only cells instead of footer view.
Step 1: Remove Footer View.
Step 2: Add Cell of Date Picker.
Step 3: When you click on Begin & End DateTime cell, then insert DateTime cell below selected cell and reload table view.
Step 4: Hope that will resolve your problem.
Let me know if you have any query.
Edit: As per discuss you only need to remove the extra cell separator by using tableFooterViewForSection.
So you only need to add below line to solve your problem:
tableView.tableFooterView = UIView(frame: CGRect(x:0, y:0, width:tableView.frame.width, heigth:0))
And Remove func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat method.
Hope it will help you.
Hello I'm trying to make the table view with custom cell. The main goal of making it is that I resize the height of tableview cell in proportion to label height which is changed by the size of text. when I run my app with below code, it looks go well. However getStringHeight methods sometimes don't recognize the line of the cell. Even if the number of line is just one, that method allocates the two line of cell which makes the cell unnecessary space.
I think the if it would recognizes two lines, line is over 3/4 filled with text in one line. I think I use Korean font included in the Apple SD Gothic (Family). But I can enter the exact name into UIFont(name: "", size: ) because it doesn't recognize it.
Can you help me? it's so important project. I stayed up all night but I haven't come up with idea to fix this problems.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
let rowNum = downloadedContents!.count - indexPath.row - 1
let sizeFactory = UINib(nibName: "DownLoadPlayViewControllerCell", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as? DownLoadPlayViewControllerCell
sizeFactory!.frame.size.height = 300
var bounds = CGSize(width: tableView.bounds.width, height: 78.5)
let pod = self.pods[rowNum]
if(sizeFactory != nil)
{
let top = sizeFactory?.programLabel.frame
let bottom = sizeFactory?.dateFilesizeLabel.frame
var labelSize = getStringHeight(pod.contentTitle!, fontSize: 13.0, width: sizeFactory!.titleLabel.frame.width)
let totalCellHeight = labelSize + top!.height + bottom!.height + 28.0
bounds.height = totalCellHeight
}
return bounds.height
}
func getStringHeight(mytext: String, fontSize: CGFloat, width: CGFloat) -> CGFloat
{
let font = UIFont(name: "Apple SD Gothic Neo", size: fontSize)
let size = CGSizeMake(width,CGFloat.max)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .ByWordWrapping;
let attributes = [NSFontAttributeName:font! as UIFont,
NSParagraphStyleAttributeName:paragraphStyle.copy()]
let text = mytext as NSString
let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil)
return rect.size.height
}
I may suggest that you use:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60
This will autosize the height of the cell without looking for the height of the text.
You can put these two lines on viewDidLoad as for the estimated row height, it helps you out in rendering.
Try this code:
Note: Both method should work.Tested in Swift 3.
Method 1:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.estimatedRowHeight = 44.0 // standard tableViewCell height
tableView.rowHeight = UITableViewAutomaticDimension
return yourArrayName.count
}
Method 2:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Note: You may need to put this code inside your cellForRowAt
yourCellName.sizeToFit()
I wanted to make custom corner rounded background for my custom cells and it worked with code below but the first row is not affected I should scroll it up to make it have a background
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row != 0 {
cell.contentView.backgroundColor = UIColor.clearColor()
var whiteRoundedCornerView: UIView = UIView(frame: CGRectMake(10, 10, 365, 250))
whiteRoundedCornerView.backgroundColor = UIColor.lightGrayColor()
whiteRoundedCornerView.layer.masksToBounds = false
whiteRoundedCornerView.layer.cornerRadius = 20
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-0, -1)
whiteRoundedCornerView.layer.shadowOpacity = 0.1
cell.contentView.addSubview(whiteRoundedCornerView)
cell.contentView.sendSubviewToBack(whiteRoundedCornerView)
}
}
The issue in your code is the if statement. When indexPath.row is 0 (which is the first row), your configuration code isn't executed. To solve this, simply remove the if statement from your code. It should look something like this:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor = UIColor.clearColor()
var whiteRoundedCornerView: UIView = UIView(frame: CGRectMake(10, 10, 365, 250))
whiteRoundedCornerView.backgroundColor = UIColor.lightGrayColor()
whiteRoundedCornerView.layer.masksToBounds = false
whiteRoundedCornerView.layer.cornerRadius = 20
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-0, -1)
whiteRoundedCornerView.layer.shadowOpacity = 0.1
cell.contentView.addSubview(whiteRoundedCornerView)
cell.contentView.sendSubviewToBack(whiteRoundedCornerView)
}
How can I add margin top to rows in tableView? Now I have:
Can I add space between rows?
My code is:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.imageView?.contentMode = .ScaleAspectFit
cell.imageView?.clipsToBounds = true
cell.imageView?.image = UIImage(data: data!)
cell.textLabel?.text = postTitle[indexPath.row]
cell.imageView?.layer.cornerRadius = 39
cell.imageView?.layer.masksToBounds = true;
return cell
}
I did try this line of code, but nothing:
cell.imageView.frame = CGRectOffset(cell.frame, 10, 10);
try this code for spacing between cells:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10; // space b/w cells
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return postTitle.count // count of items
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
let imageName = "Divider.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 1)
header.addSubview(imageView)
header.backgroundColor = UIColor.clearColor()
return header
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
Here is divider image for you.