ठीक है, मैं करने के लिए कुछ इसी तरह किया है क्या संगीत एप्लिकेशन और सुर्खियों खोज कर देता है।
मैंने UITableView को उपclass नहीं किया है, मैंने बस इसके स्क्रॉलव्यूडस्क्रॉल विधि के माध्यम से अनुभागों को ट्रैक किया है, और तालिका दृश्य के बाईं ओर हेडर दृश्य जोड़े हैं (इसलिए आपको तालिका दृश्य को अपने दृश्य में दाईं ओर रखना होगा नियंत्रक का दृश्य, जो इसका मतलब है कि आप UITableViewController का उपयोग नहीं कर सकते हैं)।
इस विधि scrollViewDidScroll, viewDidLoad में बुलाया जाना चाहिए, और didRotateFromInterfaceOrientation में:
को ध्यान में रखना आप में हेडर के आकार के लिए छोड़ दिया बराबर में जगह बनाने के लिए करना होगा कि (यदि आप रोटेशन का समर्थन) आप जिस इंटरफेस अभिविन्यास का समर्थन करते हैं।
-(CGSize)headerSize
{
return CGSizeMake(44.0f, 44.0f);
}
-(CGFloat)headerXOrigin
{
return 10.0f;
}
:
@property (nonatomic, strong) NSMutableDictionary* headerViewsDictionary;
इन तरीकों आकार और एक्स अक्ष हैडर विचारों के ऑफसेट वापसी:
-(void)updateHeadersLocation
{
for (int sectionNumber = 0; sectionNumber != [self numberOfSectionsInTableView:self.tableView]; sectionNumber++)
{
// get the rect of the section from the tableview, and convert it to it's superview's coordinates
CGRect rect = [self.tableView convertRect:[self.tableView rectForSection:sectionNumber] toView:[self.tableView superview]];
// get the intersection between the section's rect and the view's rect, this will help in knowing what portion of the section is showing
CGRect intersection = CGRectIntersection(rect, self.tableView.frame);
CGRect viewFrame = CGRectZero; // we will start off with zero
viewFrame.size = [self headerSize]; // let's set the size
viewFrame.origin.x = [self headerXOrigin];
/*
three cases:
1. the section's origin is still showing -> header view will follow the origin
2. the section's origin isn't showing but some part of the section still shows -> header view will stick to the top
3. the part of the section that's showing is not sufficient for the view's height -> will move the header view up
*/
if (rect.origin.y >= self.tableView.frame.origin.y)
{
// case 1
viewFrame.origin.y = rect.origin.y;
}
else
{
if (intersection.size.height >= viewFrame.size.height)
{
// case 2
viewFrame.origin.y = self.tableView.frame.origin.y;
}
else
{
// case 3
viewFrame.origin.y = self.tableView.frame.origin.y + intersection.size.height - viewFrame.size.height;
}
}
UIView* view = [self.headerViewsDictionary objectForKey:[NSString stringWithFormat:@"%i", sectionNumber]];
// check if the header view is needed
if (intersection.size.height == 0)
{
// not needed, remove it
if (view)
{
[view removeFromSuperview];
[self.headerViewsDictionary removeObjectForKey:[NSString stringWithFormat:@"%i", sectionNumber]];
view = nil;
}
}
else if(!view)
{
// needed, but not available, create it and add it as a subview
view = [self headerViewForSection:sectionNumber];
if (!self.headerViewsDictionary && view)
self.headerViewsDictionary = [NSMutableDictionary dictionary];
if (view)
{
[self.headerViewsDictionary setValue:view forKey:[NSString stringWithFormat:@"%i", sectionNumber]];
[self.view addSubview:view];
}
}
[view setFrame:viewFrame];
}
}
भी हम एक संपत्ति है कि विचारों कि दिखाई दे रहे हैं रखना होगा की घोषणा करने की जरूरत है
मैंने कोड बनाया है ताकि किसी भी शीर्षलेख दृश्य को जरूरी नहीं हटाया जा सके, इसलिए हमें एक ऐसी विधि की आवश्यकता है जो दृश्य को वापस लौटाए:
-(UIView*)headerViewForSection:(NSInteger)index
{
UIImageView* view = [[UIImageView alloc] init];
if (index % 2)
{
[view setImage:[UIImage imageNamed:@"call"]];
}
else
{
[view setImage:[UIImage imageNamed:@"mail"]];
}
return view;
}
यहाँ
यह कैसा दिखेगा है:
यह लैंडस्केप में कैसी दिखेंगी, मैं contraints का इस्तेमाल किया है tableView
के बाईं में 44px देने के लिए उम्मीद है कि यह मदद करता है :)।
उस ओवरलैपिंग क्विर्क को संभालने के लिए, शायद हम 'UIScrollViewDelegate'' -scrollViewDidScroll: 'के साथ प्रयास कर सकते हैं? मैं भी कर सकता हूं जो मैं कर सकता हूं, लेकिन कम से कम आपका छोटा प्रोटोटाइप वादा करता है। धन्यवाद! – Cyrille
@ माथियास मैं वांछित परिणाम प्राप्त करने में कामयाब रहा। – Besi