I created a custom UITableViewCell (I created a class that extends UITableViewCell) and I created a xib file that contains my custom cell).Everything is working great but I have the following problem. I have created the custom cell with 4 labels, and I connected the labels to code in Xcode 4. When the function for creating the table is called I can't access labels. Here the code.
`
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView,MonoTouch.Foundation.NSIndexPath indexPath)
{
string cellIdentifier = "Cell";
var cell = tableView.DequeueReusableCell (cellIdentifier);
//cell = null;
if (cell == null) {
cell = new MyCustomCell();
var views = NSBundle.MainBundle.LoadNib("MYCustomCell", cell, null);
cell = Runtime.GetNSObject( views.ValueAt(0) ) as MyCustomCell;
}
return cell;
}
Any suggestions?
You'll have to cast cell into an instance of your UITableViewCell implementation to access the custom properties and methods.
You may want to have a look at Miguel de Icaza's approach for customizing UITableViewCells and combine them with UIViews that are reusable outside of the cells.
You can find the article here: http://tirania.org/monomac/archive/2011/Jan-18.html
Related
I have 3 Labels in my prototype cell of a tableview. I want to connect them to my code and give them a name that I can use them for the cellforrow method. I connected them to the code ,but I cannot use them in this method. Does anyone know how to this?
1.Create the custom table view cell with a separate class
2.Create the UILabel as the property of custom table view cell
3.Then you can use the below code for access the UI label of your custom table view cell
let customCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell
customCell.yourLabel.text = “Success”
I have a question about cell in swift. Let's imagine that I have a cell with 20 rows. The cell is selectable right now, but the thing I want is that, when user selects 20 row it should be editable. And the value which enters the user should be saved in some variable. Is it somehow possible? Thank's in advance!
If you want 20 cells in tableView, then
You have to make nib of a cell, and make textfield in cell, and also create outlet in nib, (you can also create prototype cell)
Then in cellForRow AtIndexPath makes textField delegate as self (to class), in that class you have to implement textField delegate, and then you can access textFields as:
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView(tableView cellForRowAt: IndexPath) as! MYCELL
let text = cell.textField.text
Sorry for my bad english,
I think it will be helpful
Basically I am trying to get a value from a custom table view cell, as I have made 5 prototype cells - each with a different tableViewCell class. However, what I am trying to do is to read the data from the cell above. I have called my classes: TableViewCell1, TableViewCell2 etc... So on the cellForRowAtIndexPath, I have the code like this:
if indexPath.row == 1 {
let cell2 = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! TierCell2
return cell2
}
This works, but I have also tried to do it like this, to be able to access the value above (with no luck) - which only results in an optional value error.
let cell1:TableViewCell1 = TableViewCell1()
cell2.PriceText.text = cell1.PriceText.text
Any ideas on how I might accomplish this?
I suggest you then try creating one custom table view cell with the 5 price tier labels that you want to display. Then in the cellForRowAtIndexPath you just deque a cell as the custom class and fill in all the tier information that you already have in your dictionary and return that cell.
I added a table view to my UIViewController. then i added a label to my custom cell inside the table view. i tried to update labels inside the tableview by using tags. seems it does not working.
this is my code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
When i call the function always it does not read the tag and prints "fail". i have assigned correct tag value to my label also.
here i have attached a image of my label details in attribute inspector
Please help me the fix the issue.
As #TheAppMentor said code looks fine. Make sure you entered correct cell identifier. Then make sure you connected your view controller to correct class (it's my common mistake). Also make sure that something else in this cell hasn't the same tag. You can also print all subviews to check what's inside.
Your cell is not knowing about your label inside (the one with 500 tag). You are not providing a custom cell, because of this line:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
This is a predefined cell. As of documentation you can have predefined cells or custom ones (obtained by subclassing UITableViewCell):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
If you want to go predefined:
If you want to just put the text onto cell use (I see this one is commented on first place).
cell.textLabel?.text = object["quantity"]
Go custom
Extend UITableViewCell on a separate swift file. Do your bindings here and work with your storyboard in parallel. Assign the custom class to your cell. Also, change dequeue:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell
I made a custom tableview cell with basic style. I put webView inside content and I'm populating my cell with string on lable (works fine ), but I can't acess webView.
There is no function on "cell.webView", so i can't populate my view. How can I populate a cell with webView on custom cell?
This is how you can do this:
Step 1 : Create a custom cell by subclassing UITableViewCell
Step 2 : Add a property webView in it.
class MyTableViewCell: UITableViewCell {
var webView = UIWebView()
var companyName = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code. Add details based on your needs.
webView.frame = CGRectMake(0, 0, 200, 20)
contentView.addSubview(webView)
}
}
Step 3 : Use MyTableViewCell to supply cells for your table view. Simply call cell.webView now.