पर डिफ़ॉल्ट के बजाय UITableView अनुभाग शीर्षलेख पर क्लिक करें मैं एक ऐप पर काम कर रहा हूं जहां यह आवश्यक है कि अनुभाग का शीर्षलेख डिफ़ॉल्ट बाईं ओर दाईं ओर होना चाहिए।बाईं ओर
मैं खोज की है और कई के शौकीनों को लागू करने का सुझाव दिया:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static UIView *headerView;
if (headerView != nil)
return headerView;
NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);
// set the container width to a known value so that we can center a label in it
// it will get resized by the tableview since we set autoresizeflags
float headerWidth = 150.0f;
float padding = 10.0f; // an arbitrary amount to center the label in the container
headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// create the label centered in the container, then set the appropriate autoresize mask
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, headerWidth - 2.0f * padding, 44.0f)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
headerLabel.textAlignment = UITextAlignmentRight;
headerLabel.text = headerText;
[headerView addSubview:headerLabel];
return headerView;
}
हालांकि, जब मैं बढ़ाने की कोशिश हैडर दृश्य फ्रेम का x- निर्देशांक, यह देखने की स्थिति प्रभावित नहीं करता। और यह हमेशा तालिका दृश्य के केंद्र में है।
मुझे दाईं ओर होने के लिए शीर्षलेख की आवश्यकता है। सब से
बजाय एक flexibleWidth के साथ जा रहा है, तो आप पूरी चौड़ाई होने के लिए यह मजबूर नहीं कर सकते हैं? फिर दृश्य पूर्ण चौड़ाई का उपभोग करेगा और पाठ को सही ठहराएगा। – bryanmac
आपने पहले से ही एक उत्तर चुना है जो अधिकतर सही है, लेकिन मार्जिन प्राप्त करने के लिए रिक्त स्थान जोड़ना एक वास्तविक हैक है। अपनी समस्या को हल करने का सही तरीका एक खाली दृश्य (या पृष्ठभूमि रंग वाला एक) बनाना है और हेडर व्यू के रूप में इसका उपयोग करना है। फिर आप उस कंटेनर व्यू में UILabel जोड़ते हैं, और इसे दाएं तरफ थोड़ा सा ऑफसेट करते हैं। यह अवधारणा - अन्य विचारों को रखने के लिए एक कंटेनर के रूप में एक खाली दृश्य का उपयोग करना एक आम और उपयोगी उपकरण है। –
@ डेविड आप सही हैं। रिक्त स्थान जोड़ने का विचार अच्छा नहीं है। हालांकि यह मेरे लिए समय के लिए काम किया।जैसा कि मैंने अपने प्रश्न में उल्लेख किया था कि दृश्य हमेशा केंद्र पर हो रहा था, जहां मुझे इसे सही होने की आवश्यकता है। फ्रेम सेट करना काम नहीं कर रहा था। इसके अलावा लेबल दाएं गठबंधन था। मैं समझता हूं कि UIView का उपयोग करना अधिक अनुकूलन विकल्प रखने के लिए बहुत बेहतर है। टिप्पणी के लिए धन्यवाद! –