In the following function:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 10
}
is the second argument the result returned by calling the function numberOfRowsInSection with section as the argument? If so, where is it getting section from?
This syntax allows to set a name for an argument that is different from the name of the local variable used to capture that arguments value.
numberOfRowsInSection is the name of the parameter that you use when calling this function.
section is the name of the local variable that holds the value that got passed.
So you would call this function like so:
Int rowCount = tableView(tableView: aTabelView, numberOfRowsInSection: 10);
And in that function, you would use the argument like so:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return section;
}
numberOfRowsInSection is the argument name when calling the function. section is the argument name from within the function. So, for example, you would call this function as follows:
myObject.tableView(aView, numberOfRowsInSection:4)
but from within the function, you would refer to that 4 as follows:
let valueOfSectionArgument = section
Related
i have variable of type [Int: [CustomObject]], and i need to count [CustomObject].count by key in my numberOfRowsInSection ? how can i access this value ?
var tasks = [Int :[TaskEntity]]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("section: \(section), taskKey: \(tasks.keys.sorted())")
}
Like each key in dict has an array of elements as value, i need to count this elements in array for each key-value pair
UPD: I'he added print line as Joakim Danielson suggested. Print says:
When i add cell to section 0: "section: 0, taskKey: []
section: 0, taskKey: [0]"
When i add cell to section 1: "section: 1, taskKey: [0, 1]
section: 0, taskKey: [0, 1]"
Now i'm confused about output
I am creating an iOS Swift 3 app and in this app I have a tableview with data coming from an API. I want to display this data in the tableview but I want to do it grouped by firstname. I have managed to first group the data (see below) but xCode is complaining that XXX.
This is the declaration of the sections:
var sections = [String: [User]]()
This is how I group the data:
self.sections = Dictionary(grouping: self.contacts.filter({ !$0.firstname!.isEmpty })) {
$0.firstname!.prefix(1).capitalized
}
This is my output:
["D": [motto.User(id: Optional(1), firstname: Optional("Dan"), lastname: Optional("Meeler"), email: Optional("time#example.com"))], "M": [coiqo.User(id: Optional(3), firstname: Optional("Mia"), lastname: Optional("Kallas"), email: Optional("mia#ka.com"))]]
This is the error I got:
Cannot subscript a value of type '[String : [User]]' with an index of type 'Int'
In this function for tableview:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].count
}
How can I get this array to work in a tableview?
Update
I just had to add this to dasblinkenlight answer and it worked!
if(groupKeys.count > 0) {
return sections[groupKeys[section]]!.count
} else {
return sections.count
}
Your self.sections is Dictionary, and dictionaries are unordered.
Once you've made user groups from API results, make a separate array composed of group keys:
let groupKeys : [String] = Array(self.sections.keys)
Sort that array in the way that you wish your sections to appear (alphabetical, reverse alphabetical, natural, etc.)
Now you can write your function like this:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[groupKeys[section]].count
}
This question already has an answer here:
What are the new "for", "at", "in" keywords in Swift3 function declarations?
(1 answer)
Closed 6 years ago.
I have just opened a swift 2 project with last Xcode version.
Xcode suggest me to auto convert my code to swift 3.
Here is something strange:
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
func tableView(_ tableView: MyTableView, numberOfRowsInSection section: Int) -> Int
{
return 10
}
If you look, Xcode added a "in" keyword for the numberOfSections method. But he did nothing for the numberOfRowsInSection method.
I do not understand why.
What is this "in" keyword in Swift 3 ?
It's not a keyword. It's the name of the first parameter in the method.
In Swift2 the method was...
func numberOfSections(inTableView: UITableView) -> Int
... and it would be called like...
numberOfSections(tableView)
In Swift 3 there is a lot of emphasis on making function names more verb based and removing redundant inferred or implied type from the method names. Also, the first parameter name of methods is now required.
All these changes lead to the name being updated to what you have seen and the call becomes...
numberOfSections(in: tableView)
You can read more about the changes in Swift 3 in Apple's API Guidelines.
I am still new to iOS programming and trying to understand the logic behind the program. I am trying to call a function inside of the tableView function. I just want a piece of code inside of the tableView to run at a certain point. For a simple example
override func viewDidLoad() {
tableView.testFunction()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
func testFunction() {
println("test")
}
...
}
But I get the error that I cannot invoke function with no arguments. I am new to swift so I apologize for the very basic question.
One function cannot call a function that is inside another function.
You have defined testFunction() as a local function inside your tableView:cellForRowAtIndexPath: function. No other function can see it; it is visible only inside tableView:cellForRowAtIndexPath: (and only to later code).
See my book:
A function declared in the body of a function (also called a local function) is available to be called by later code within the same scope, but is completely invisible outside its scope.
I have the following model:
class Word: NSManagedObject {
#NSManaged var definitions: NSSet
}
Then, in one of my view controllers, I have the following property:
var word: Word?
However, if I try to do this:
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return self.word?.definitions.count
}
I get an error:
Value of optional type 'Int?' not unwrapped; did you mean to use '!'
or '?'?
According to the documentation, the value of an optional chaining call is also optional, so .count should return an optional Int. So I tried unwrapping it:
return self.word?.definitions.count!
But that still results in the same error. What's the right way to do this?
The problem is the order of operations. The cleanest way to do this would be to stop optional chaining (if you really don't want to handle the nil case).
return self.word!.definitions.count
But I recommend doing this for safety:
if let actualWord = self.word {
return actualWord.definitions.count
}
else {
return 0
}
Or even shorter:
return self.word ? self.word!.definitions.count : 0
Otherwise, you might as well declare word as an implicitly unwrapped optional and not have to worry about it:
var word: Word!
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return self.word.definitions.count
}