जवाबों:
आप हाइलाइट रंग को कई तरीकों से बदल सकते हैं।
अपनी सेल की चयन संपत्ति बदलें। यदि आप इसे बदलते हैं UITableViewCellSelectionStyleGray, तो यह ग्रे होगा।
selectedBackgroundViewसंपत्ति बदलें । दरअसल ब्लू ढाल बनाने वाला एक दृश्य है। आप एक दृश्य बना सकते हैं और अपनी पसंद की चीज़ों को आकर्षित कर सकते हैं, और अपने टेबल व्यू सेल्स की पृष्ठभूमि के रूप में दृश्य का उपयोग कर सकते हैं।
selectedBackgroundViewतब सिर्फ सादा कभी दिखाई नहीं देगा। "सिलेक्शन" को डिफ़ॉल्ट पर सेट करने के बाद ही selectedBackgroundViewशो अप करें। IOS 6 और 7. पर परीक्षण किया गया
cell.selectedBackgroundView = [UIView new];और आप जो भी रंग चाहते हैं उसे सेट करें:cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHex:@"ecf2f5" andAlpha:1];
ज़ोनल पहले से ही एक उत्कृष्ट उत्तर प्रदान कर चुका है। मैंने सोचा UIViewकि टेबलव्यू सेल में एक छोटे कोड स्निपेट को शामिल करना उपयोगी हो सकता है जो चयनित पृष्ठभूमि दृश्य के रूप में प्रस्तुत होगा।
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
UITableViewCellselectedBackgroundViewको सेट किया UIViewजो मैंने अपने चुने हुए पृष्ठभूमि रंग के साथ बनायाइसने मेरे लिए अच्छा काम किया। टिप Zonble के लिए धन्यवाद।
UITableViewCell तीन डिफ़ॉल्ट चयन शैली हैं: -
कार्यान्वयन इस प्रकार है: -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
स्विफ्ट में, इस में उपयोग करें cellForRowAtIndexPath
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
यदि आप चाहते हैं कि आपका चयन रंग हर में समान हो UITableViewCell, तो इस का उपयोग करें AppDelegate।
let selectedView = UIView()
selectedView.backgroundColor = .white
UITableViewCell.appearance().selectedBackgroundView = selectedView
यदि आप इसे ऐप वाइड में बदलना चाहते हैं, तो आप अपने ऐप डेलिगेट में तर्क जोड़ सकते हैं
class AppDelegate: UIResponder, UIApplicationDelegate {
//... truncated
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// set up your background color view
let colorView = UIView()
colorView.backgroundColor = UIColor.yellowColor()
// use UITableViewCell.appearance() to configure
// the default appearance of all UITableViewCells in your app
UITableViewCell.appearance().selectedBackgroundView = colorView
return true
}
//... truncated
}
पूर्णता के लिए: यदि आपने अपना स्वयं का उपवर्ग बनाया है UITableViewCellतो - (void)setSelected:(BOOL)selected animated:(BOOL)animatedविधि को लागू कर सकते हैं , और सामग्री दृश्य में आपके द्वारा जोड़े गए कुछ दृश्य की पृष्ठभूमि का रंग निर्धारित कर सकते हैं। (यदि ऐसा है) या कंटेंटव्यू के ही (यदि यह आपके अपने विचारों में से एक द्वारा कवर नहीं है)।
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected) {
self.contentView.backgroundColor = UIColor.blueColor;
} else {
self.contentView.backgroundColor = UIColor.whiteColor;
}
}
(स्रोत कोड DIV के छोटे चौड़ाई फिट करने के लिए उपयोग नहीं किया है :)
इस दृष्टिकोण को चयनित बैकग्राउंड व्यू का उपयोग करने के दो फायदे हैं, यह कम मेमोरी और थोड़ा कम सीपीयू का उपयोग करता है, न कि यू तब तक नोटिस करेगा जब तक कि यू सैकड़ों सेल प्रदर्शित न करें।
selectedBackgroundView बाद ही विधि जोड़ने की शुरुआत में काम करता हैself. selectedBackgroundView = [UIView new];
selectedBackgroundViewसंपत्ति की कोई सेटिंग नहीं है ।
मुझे UITableViewCellSelectionStyleDefaultकाम करने के लिए कस्टम पृष्ठभूमि रंग के लिए चयन शैली निर्धारित करनी है । यदि कोई अन्य शैली है, तो कस्टम पृष्ठभूमि रंग को अनदेखा किया जाएगा। आईओएस 8 पर परीक्षण किया गया।
सेल के लिए पूर्ण कोड निम्नानुसार है:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// This is how you change the background color
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
@ उपयोगकर्ता के उत्तर के आधार पर , आप अपने ऐप कोड में कहीं भी इस एक्सटेंशन को जोड़ सकते हैं और अपने चयन रंग को सीधे अपने ऐप की प्रत्येक कोशिकाओं के लिए स्टोरीबोर्ड एडिटर में रख सकते हैं:
@IBDesignable extension UITableViewCell {
@IBInspectable var selectedColor: UIColor? {
set {
if let color = newValue {
selectedBackgroundView = UIView()
selectedBackgroundView!.backgroundColor = color
} else {
selectedBackgroundView = nil
}
}
get {
return selectedBackgroundView?.backgroundColor
}
}
}
@IBDesignable class UIDesignableTableViewCell: UITableViewCell {
@IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
didSet {
selectedBackgroundView = UIView()
selectedBackgroundView?.backgroundColor = selectedColor
}
}
}
अपने स्टोरीबोर्ड में, अपने UITableViewCell के वर्ग को UIDesignableTableViewCell पर सेट करें, गुण निरीक्षक पर, आप अपने सेल के चयनित रंग को किसी भी रंग में बदल सकते हैं।
आप इसका उपयोग अपनी सभी कोशिकाओं के लिए कर सकते हैं। यह आपके गुण निरीक्षक की तरह दिखेगा।
UIColor.clear, नहीं UIColor.clearColor()। यकीन नहीं होता है कि अगर स्विफ्ट में भी बदलाव होता है, लेकिन यह भी बिना काम करता है @IBDesignable।
स्विफ्ट 3.0 / 4.0
यदि आपने अपना स्वयं का कस्टम सेल बनाया है, तो आप सभी सेल के लिए चयन रंग बदल सकते हैं awakeFromNib():
override func awakeFromNib() {
super.awakeFromNib()
let colorView = UIView()
colorView.backgroundColor = UIColor.orange.withAlphaComponent(0.4)
self.selectedBackgroundView = colorView
}