我有一个 tableview 单元格的子类。
class profileTableViewCell: UITableViewCell {
@IBOutlet weak var tableviewUsernameLabel: UILabel!
@IBOutlet weak var tableviewMessageLabel: UILabel!
@IBOutlet weak var tableviewTimeStamp: UILabel!
}
我需要在我的 View Controller 中访问这些标签。我该怎么做。我对编程相当陌生。
-我在下面的标签上明显地看到“未解析的标识符”。我如何访问这些标签?
-也尝试将 cell.tableViewUsername.text (cell.) 放在所有标签的前面仍然不起作用。
class UserProfile: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = profileTableView.dequeueReusableCell(withIdentifier: "profileCell")
//Set username label to display username
let usernameLabel = cell?.viewWithTag(1) as! UILabel
tableviewUsernameLabel.text = profileDataArray[indexPath.row].username
//Set message label to display message
let messageLabel = cell?.viewWithTag(2) as! UILabel
tableviewMessageLabel.text = profileDataArray[indexPath.row].message
tableviewMessageLabel.numberOfLines = 0
//Set timeStampLabel to current time AGO
let timeStampLabel = cell?.viewWithTag(4) as! UILabel
tableviewTimeStamp.text = profileDataArray[indexPath.row].timeStamp
tableviewTimeStamp.numberOfLines = 0
return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profileDataArray.count // your number of cell here
}
}
最佳答案
你必须type cast dequeueReusableCell(withIdentifier:) 返回到 profileTableViewCell 的单元格。
let cell = profileTableView.dequeueReusableCell(withIdentifier: "profileCell") as! profileTableViewCell
cell.tableviewUsernameLabel.text = ...
cell.tableviewMessageLabel.text = ...
cell.tableviewTimeStamp.text = ...
return cell
关于ios - swift 。 Viewcontroller 知道子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267750/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我正在研究使用EventMachine支持的twitter-streamrubygem来跟踪和捕获推文。我对整个事件编程有点陌生。我如何判断我在事件循环中所做的任何处理是否导致我落后?有没有简单的检查方法? 最佳答案 您可以通过使用周期性计时器并打印出耗时来确定延迟。如果您使用的是1秒的计时器,您应该已经过了大约1秒,如果它更长,您就知道您正在减慢react器的速度。@last=Time.now.to_fEM.add_periodic_timer(1)doputs"LATENCY:#{Time.now.to_f-@last}"@
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
这里还有一个新手问题:require'tasks/rails'我在每个Rails项目的根路径中的Rakefile中看到了这一行。我猜这行用于要求vendor/rails/railties/lib/tasks/rails.rb加载所有rake任务:$VERBOSE=nil#LoadRailsrakefileextensionsDir["#{File.dirname(__FILE__)}/*.rake"].each{|ext|loadext}#LoadanycustomrakefileextensionsDir["#{RAILS_ROOT}/lib/tasks/**/*.rake"].so
我可以访问ruby的异常层次结构(它在镐和蜂鸟中都提到过),但我不确定使用哪个异常,因为我没有找到关于每个术语含义的任何信息。使用正确的异常类重要吗? 最佳答案 创建您自己的异常时很重要。一个重要的警告是,继承自Exception而不是StandardError(常见错误)的异常不会被rescue捕获(没有任何参数)。 关于ruby-在ruby中使用正确的异常子类,我们在StackOverflow上找到一个类似的问题: https://stackove