क्या आप यह चाहते हैं?
import UIKit
class ViewController: UITableViewController {
var data = [[String]]()
var headerOrigins = [CGPoint]()
var floatingHeaders = [UIView]()
override func viewDidLoad() {
super.viewDidLoad()
data = [["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten"], ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten"], ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten"]]
tableView.dataSource = self
tableView.delegate = self
for i in 0..<data.count {
headerOrigins += tableView(tableView, cellForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: i)).frame.origin
let floatingHeader = UIView(frame: CGRectMake(0, headerOrigins[i].y, tableView.frame.size.width, 44))
floatingHeader.backgroundColor = UIColor.redColor()
floatingHeader.alpha = 0.5
let label = UILabel(frame: CGRectMake(100, 10, 100, 30))
label.text = "Header \(i)"
floatingHeader.addSubview(label)
tableView.addSubview(floatingHeader)
floatingHeaders += floatingHeader
}
}
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return data.count
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = data[indexPath.section][indexPath.row]
return cell
}
override func scrollViewDidScroll(scrollView: UIScrollView!) {
var tableTop = tableView.bounds.origin.y + tableView.scrollIndicatorInsets.top
if tableTop < 0 {
floatingHeaders[0].frame.origin.y = 0
} else {
for i in 0..<data.count {
if tableTop > headerOrigins[i].y {
floatingHeaders[i].frame.origin.y = tableTop
} else if tableTop < headerOrigins[i].y {
floatingHeaders[i].frame.origin.y = headerOrigins[i].y
}
var j = i
while j > 0 {
if (CGRectGetMaxY(floatingHeaders[j-1].frame) > floatingHeaders[j].frame.origin.y) {
floatingHeaders[j-1].frame.origin.y = floatingHeaders[j].frame.origin.y - floatingHeaders[j].frame.size.height
}
j--
}
}
}
}
स्रोत
2014-07-12 12:53:20
क्या किसी ने इसे स्वीकार किया है? मैं अभी भी इसके साथ संघर्ष कर रहा हूं – Ryan
सभी कोशिकाओं के लिए आपका क्या मतलब है? आप सभी कोशिकाओं के लिए पारदर्शी शीर्षलेख क्यों चाहते हैं? ViewForHeaderInSection से कम अस्पष्टता के साथ एक दृश्य लौटने में क्या गलत है? और व्यवहार के बारे में क्या है जैसे उपयोगकर्ता पहले ही स्क्रॉल कर चुका था, आप पुन: पेश करना चाहते हैं? क्या यह मुझे या आपकी व्याख्या है? lol – believesInSanta
ऐसा लगता है कि आप सिर्फ एक सादे UITableViewStyle शीर्षलेख चाहते हैं जो पारदर्शी है? – believesInSanta