我试图总计数据表列的总小时数和分钟数。
我在DT细胞中的这种格式:4h 12m
然后像:
4小时12分钟 2小时8分钟 4小时0分总数必须是:
10h 20m
我尝试使用类似的东西:
final class Something : UIViewController {
fileprivate var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = UITableView(frame: CGRect.zero, style: .plain)
self.tableView.translatesAutoresizingMaskIntoConstraints = false
//Delegate, register cell, ...
self.view.addSubview(self.tableView)
let views/*: [String: Any]*/ = ["table": self.tableView]
//THIS LINE NOW WILL CRASH
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-0-[table]-0-|", options: [], metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[table]-0-|", options: [], metrics: nil, views: views))
}
}
我需要帮助#34; perTemp"功能
答案 0 :(得分:0)
var a = ["4h 12m 2h 8m 4h 0m", "4h 12m"];
var s = a.join(","); // "4h 12m 2h 8m 4h 0m,4h 12m"
var re = /([\d]*?(h|m))/gmi;
var res = s.match(re); // Array [ "4h", "12m", "2h", "8m", "4h", "0m", "4h", "12m" ]
var hours = res.filter(function(value){
if(value.indexOf("h") > -1){
return true;
}
}).map(function(value){
return parseInt(value.substr(0, value.length-1))
}).reduce(function(pre, cur){
return pre + cur
}); // Array [ "4h", "2h", "4h", "4h" ] -> Array [ 4, 2, 4, 4 ] -> 14
var min = res.filter(function(value){
if(value.indexOf("m") > -1){
return true;
}
}).map(function(value){
return parseInt(value.substr(0, value.length-1))
}).reduce(function(pre, cur){
return pre + cur
}); // 32
// add hour if > 60 min
hour += Math.floor(min/60);
min = min % 60;