जैसा कि निम्नलिखित छवि में दिखाया गया है, मैं एक टिप (सबसे बड़े UIView का सबव्यू) जोड़ना चाहता हूं जो UITableViewCell में UIStackView के साथ संरेखित हो। UITableViewCell में UIStackView की चौड़ाई गतिशील है, इसलिए मुझे UITableViewCell लेआउट के सबव्यू के बाद टिप व्यू रखने की आवश्यकता है।
मैंने willDisplayCell फ़ंक्शन में टिप व्यू जोड़ने का प्रयास किया है, लेकिन मुझे वहां जो स्थिति मिलती है वह अंतिम स्थिति नहीं है।
साथ ही, मैंने UITableViewCell के लेआउटसबव्यू फ़ंक्शन में टिप व्यू की स्थिति सेट करने का प्रयास किया। चूंकि इस फ़ंक्शन को कई बार कहा जाता है, टिप अंततः अपेक्षित स्थान पर जाएगी, लेकिन शुरुआत में यह झिलमिलाहट करेगी।
तो मुझे किस फंक्शन में टिप व्यू जोड़ना चाहिए?
-------- संपादित----------
इसे स्पष्ट करने के लिए एक कोड स्निपेट जोड़ें। मैं सिर्फ यह जानना चाहता हूं कि मुझे निम्नलिखित कोड कहां रखना चाहिए, ताकि rect.origin.x स्टैकव्यू की अंतिम स्थिति हो।
UIView *view = self.view;
CGRect rect = [cell.stackView convertRect:cell.stackView.bounds toView:view];
[self.tipView.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:rect.origin.x].active = YES;
[self.tipView.trailingAnchor constraintLessThanOrEqualToAnchor:view.trailingAnchor constant:-marginConstant].active = YES;
[self.tipView.bottomAnchor constraintEqualToAnchor:self.tableView.bottomAnchor constant:-TSkBubbleClickAreaHeight-marginConstant].active = YES;
[view layoutIfNeeded];
1 उत्तर
संपादित करें
कोशिश करने और सीधे अपने प्रश्न का उत्तर देने के लिए, आप जानते हैं कि सेल की सामग्री का लेआउट पूरी तरह से समाप्त हो गया है जब आप सेल में जो कुछ भी कर रहे हैं वह किया जाता है। इसलिए, यह जानने के लिए कि आपके सेल का स्टैक व्यू कब निर्धारित किया गया है, आप स्टैक व्यू को उपवर्गित कर सकते हैं और उस उपवर्ग में layoutSubviews
लागू कर सकते हैं।
चूंकि यह अभी भी स्पष्ट नहीं है कि बिल्कुल आप क्या कर रहे हैं, मैं कुछ तरीकों की पेशकश करूंगा ...
यदि आप टेबल व्यू पर reloadData
को कॉल कर रहे हैं, तो आप इसे आजमा सकते हैं:
[tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[self showTipView:pth];
});
यदि आप insertRowsAtIndexPaths
को कॉल कर रहे हैं, तो आप इसे आजमा सकते हैं:
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self showTipView:pth];
}];
[self->tableView insertRowsAtIndexPaths:@[pth] withRowAnimation:UITableViewRowAnimationRight];
[CATransaction commit];
यहां एक उदाहरण दिया गया है (मैंने इसे सरल रखने की कोशिश की है, और स्पष्ट टिप्पणियां प्रदान करता हूं):
ExampleTableViewCell.h
//
// ExampleTableViewCell.h
// Created by Don Mag on 9/15/20.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ExampleTableViewCell : UITableViewCell
- (void) configureCell:(NSString *)s;
- (UIStackView *)getStackRef;
@end
NS_ASSUME_NONNULL_END
ExampleTableViewCell.m
//
// ExampleTableViewCell.m
// Created by Don Mag on 9/15/20.
//
#import "ExampleTableViewCell.h"
@interface ExampleTableViewCell ()
{
UIStackView *stackView;
UILabel *label;
}
@end
@implementation ExampleTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self commonInit];
}
return self;
}
- (void) commonInit {
label = [UILabel new];
label.backgroundColor = [UIColor yellowColor];
stackView = [UIStackView new];
stackView.translatesAutoresizingMaskIntoConstraints = NO;
[stackView addArrangedSubview:label];
[self.contentView addSubview:stackView];
UILayoutGuide *g = self.contentView.layoutMarginsGuide;
[NSLayoutConstraint activateConstraints:@[
// constrain stack view Top: 0 / Trailing: 0 / Bottom: 0
[stackView.topAnchor constraintEqualToAnchor:g.topAnchor constant:0.0],
[stackView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],
[stackView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:0.0],
]];
}
- (void) configureCell:(NSString *)s {
label.text = s;
}
- (UIStackView *)getStackRef {
return stackView;
}
@end
ExampleViewController.h
//
// ExampleViewController.h
// Created by Don Mag on 9/15/20.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ExampleViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
ExampleViewController.m
//
// ExampleViewController.m
// Created by Don Mag on 9/15/20.
//
#import "ExampleViewController.h"
#import "ExampleTableViewCell.h"
@interface ExampleViewController () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
{
NSMutableArray *myData;
NSMutableArray *sampleStrings;
UIView *tipView;
UIButton *reloadButton;
UIButton *insertButton;
UITableView *tableView;
NSInteger idx;
NSInteger insertRow;
}
@end
@implementation ExampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
// row number to insert and show the tipView
insertRow = 7;
// init some sample data
myData = [NSMutableArray new];
for (int i = 0; i < 30; i++) {
NSString *s = [NSString stringWithFormat:@"Row %i", i];
[myData addObject:s];
}
// a few example strings
sampleStrings = [NSMutableArray new];
[sampleStrings addObject:@"Short text example."];
[sampleStrings addObject:@"A little more text example."];
[sampleStrings addObject:@"Considerably longer text for this example."];
// index for sampleStrings array
idx = -1;
// create a "tip view"
// red background view with
// green background label
// label is inset 4-pts on each side
tipView = [UIView new];
tipView.backgroundColor = [UIColor redColor];
UILabel *tipLabel = [UILabel new];
tipLabel.backgroundColor = [UIColor greenColor];
tipLabel.text = @"This is the tip!";
tipView.translatesAutoresizingMaskIntoConstraints = NO;
tipLabel.translatesAutoresizingMaskIntoConstraints = NO;
[tipView addSubview:tipLabel];
[NSLayoutConstraint activateConstraints:@[
[tipLabel.topAnchor constraintEqualToAnchor:tipView.topAnchor constant:4.0],
[tipLabel.leadingAnchor constraintEqualToAnchor:tipView.leadingAnchor constant:4.0],
[tipLabel.bottomAnchor constraintEqualToAnchor:tipView.bottomAnchor constant:-4.0],
[tipLabel.trailingAnchor constraintEqualToAnchor:tipView.trailingAnchor constant:-4.0],
]];
// init buttons
reloadButton = [UIButton new];
[reloadButton setTitle:@"Reload" forState:UIControlStateNormal];
[reloadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[reloadButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[reloadButton setBackgroundColor:[UIColor blueColor]];
insertButton = [UIButton new];
[insertButton setTitle:@"Insert" forState:UIControlStateNormal];
[insertButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[insertButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[insertButton setBackgroundColor:[UIColor blueColor]];
// init table view
tableView = [UITableView new];
for (UIView *v in @[reloadButton, insertButton, tableView]) {
v.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:v];
}
UILayoutGuide *g = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
// buttons at top
[reloadButton.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
[reloadButton.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:24.0],
[insertButton.topAnchor constraintEqualToAnchor:g.topAnchor constant:20.0],
[insertButton.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-24.0],
[insertButton.leadingAnchor constraintEqualToAnchor:reloadButton.trailingAnchor constant:20.0],
[insertButton.widthAnchor constraintEqualToAnchor:reloadButton.widthAnchor],
// constrain tableView Top: 20-pts from buttons / Leading: 0 / Trailing: 0 / Bottom: 0
[tableView.topAnchor constraintEqualToAnchor:reloadButton.bottomAnchor constant:20.0],
[tableView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
[tableView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],
[tableView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:0.0],
]];
[tableView registerClass:[ExampleTableViewCell class] forCellReuseIdentifier:@"exCell"];
tableView.delegate = self;
tableView.dataSource = self;
[reloadButton addTarget:self action:@selector(btnTap:) forControlEvents:UIControlEventTouchUpInside];
[insertButton addTarget:self action:@selector(btnTap:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[tipView removeFromSuperview];
}
- (void)btnTap:(UIButton *)btn {
// remove tipView if it's showing
[tipView removeFromSuperview];
NSString *s = sampleStrings[++idx % 3];
if (btn == reloadButton) {
[self reloadMethod:s];
} else {
[self insertMethod:s];
}
}
- (void)insertMethod:(NSString *)s {
// IndexPath for cell you want
__block NSIndexPath *pth = [NSIndexPath indexPathForRow:insertRow inSection:0];
[myData insertObject:s atIndex:pth.row];
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self showTipView:pth];
}];
[self->tableView insertRowsAtIndexPaths:@[pth] withRowAnimation:UITableViewRowAnimationRight];
[CATransaction commit];
}
- (void)reloadMethod:(NSString *)s {
// IndexPath for cell you want
__block NSIndexPath *pth = [NSIndexPath indexPathForRow:insertRow inSection:0];
[myData insertObject:s atIndex:pth.row];
[tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[self showTipView:pth];
});
}
- (void)showTipView:(NSIndexPath *)pth {
// get a reference to the cell
ExampleTableViewCell *cell = [tableView cellForRowAtIndexPath:pth];
// if row is not visible
if (!cell) {
return;
}
// get a reference to the cell's stack view
UIStackView *stack = [cell getStackRef];
// add tipView to self.view
[self.view addSubview:tipView];
// constrain tipView
// Leading to stack view's Leading
// Bottom to cell's Top - 4
[NSLayoutConstraint activateConstraints:@[
[tipView.leadingAnchor constraintEqualToAnchor:stack.leadingAnchor constant:0.0],
[tipView.bottomAnchor constraintEqualToAnchor:cell.topAnchor constant:-4.0],
]];
// if we want to "fade-in" the tipView
[tipView setAlpha:0.0];
[UIView animateWithDuration:0.3
animations:^{
[self->tipView setAlpha:1.0];
}];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
ExampleTableViewCell *c = (ExampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"exCell" forIndexPath:indexPath];
[c configureCell:myData[indexPath.row]];
return c;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myData count];
}
@end
संबंधित सवाल
नए सवाल
ios
iOS, Apple iPhone, iPod टच और iPad पर चलने वाला मोबाइल ऑपरेटिंग सिस्टम है। IOS प्लेटफॉर्म पर प्रोग्रामिंग से संबंधित प्रश्नों के लिए इस टैग [ios] का उपयोग करें। उन प्रोग्रामिंग भाषाओं के लिए विशिष्ट मुद्दों के लिए संबंधित टैग [उद्देश्य-सी] और [स्विफ्ट] का उपयोग करें।