2009-04-08 17 views
11

को बैज जोड़ें मैं कैसे UITableViewCell में एक बैज जोड़ है, इस तरह:आईओएस - UITableViewCell

alt text http://img17.imageshack.us/img17/9974/img0001ac9.png

मैं बस एक पाठ है और उस पर लेबल के साथ एक subview जोड़ना चाहिए?

+0

[यहाँ] (http://www.spaulus.com/2010/09/create-a-custom-iphone-ipad-badge/?lang=en) मेरी खुद की एक alternativ है। का आनंद लें! – Sascha

उत्तर

1

हां, वर्तमान में UITableView सेल में बैज जोड़ने का कोई समर्थित तरीका नहीं है। इस उदाहरण में, यह एक कस्टम सबव्यूव है जिसमें एक छवि और यूलाबेल शामिल है।

1

मैं अनुकूलित बैज बनाने के लिए एक और विकल्प जोड़ना चाहता हूं। CustomBadge थोड़ा और अधिक शक्तिशाली है। यह खुला और मुफ्त है।

2

मेरे लिए सबसे सरल तरीका cell.accessoryView का उपयोग करना है। मैं इसे कैसे किया कृपया मेरे कोड पर गौर:

UIImageView * commentsViewBG = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"counter1.png"]]; 
commentsViewBG.frame = CGRectMake(
    commentsViewBG.frame.origin.x, 
    commentsViewBG.frame.origin.y, 30, 20); 


UILabel *commentsCount; 
if (commentsArray.totalCount < 10) 
    commentsCount = [[UILabel alloc]initWithFrame:CGRectMake(10, -10, 40, 40)]; 
else if (commentsArray.totalCount < 100) 
    commentsCount = [[UILabel alloc]initWithFrame:CGRectMake(5, -10, 40, 40)]; 
else if (commentsArray.totalCount < 1000) 
{ 
    commentsViewBG.frame = CGRectMake(
     commentsViewBG.frame.origin.x, 
     commentsViewBG.frame.origin.y, 40, 20); 
    commentsCount = [[UILabel alloc]initWithFrame:CGRectMake(5, -10, 40, 40)]; 
} 
commentsCount.text = [NSString stringWithFormat:@"%ld",(long)commentsArray.totalCount]; 
commentsCount.textColor = [UIColor whiteColor]; 
commentsCount.backgroundColor = [UIColor clearColor]; 
[commentsViewBG addSubview:commentsCount]; 
cell.accessoryView = commentsViewBG; 

और मेरे परिणाम:

enter image description here

आशा है कि यह मदद करता है।

+0

कृपया कई प्रश्नों के लिए एक ही जवाब पोस्ट न करें: यह या तो सभी के लिए उपयुक्त नहीं है या प्रश्न डुप्लिकेट हैं जिन्हें फ़्लैग/बंद किया जाना चाहिए। –

+0

यदि यह खोजना आसान बनाता है, तो मुझे बहुत समस्या नहीं दिखाई देती है। कभी-कभी डुप्लीकेट प्रश्न Google पर पहले दिखाई देते हैं, और यहां तक ​​कि अगर उन्हें ध्वजांकित किया जाता है, तो वे कभी-कभी बेहतर उत्तर प्राप्त कर सकते हैं। – Samin

2

TDBadgedCell एक बहुत अच्छी पसंद है। आपकी जरूरतों के लिए अत्यधिक अनुकूलन योग्य।

4

यहां @ पीओएफ के उत्तर में तेजी से वृद्धि हुई है। हम के रूप में कई subviews की जरूरत नहीं है और हम एन अंक, समर्थन करने के लिए गणित का उपयोग कर सकते न सिर्फ 1-3:

func setDiscountBadge(count: Int) { 
    let size: CGFloat = 26 
    let digits = CGFloat(count("\(number)")) // digits in the label 
    let width = max(size, 0.7 * size * digits) // perfect circle is smallest allowed 
    let badge = UILabel(frame: CGRectMake(0, 0, width, size)) 
    badge.text = "\(number)" 
    badge.layer.cornerRadius = size/2 
    badge.layer.masksToBounds = true 
    badge.textAlignment = .Center 
    badge.textColor = UIColor.whiteColor() 
    badge.backgroundColor = cfg.UIColors.brand 
    YOURCELL.accessoryView = badge // !! change this line 
} 

और परिणाम (मैं एक ब्रांड रंग का उपयोग करें, लेकिन तुम्हारा किसी भी रंग हो सकता है):

uilabel badge