का उपयोग कर एक कस्टम UITableViewCell प्रोग्रामेटिक रूप से कैसे बनाएं I एक UITableView को लागू करने का प्रयास कर रहा हूं जो एक ट्विटर क्लाइंट की समयरेखा के समान व्यवहार करेगा। अभी मैं बस UITableViewCell के अंदर दो लेबल प्राप्त करने का प्रयास कर रहा हूं। जैसा कि this Stack Overflow answer द्वारा अनुशंसित किया गया है, मैं प्रत्येक लेआउट के लिए एक अलग पुन: उपयोगकर्ता का उपयोग कर रहा हूं। मेरे लेआउट सरल हैं, जिनमें से एक या तो लेबल या दो लेबल शामिल हैं। आखिर में मैं UITableViewCells की ऊंचाई समायोजित करूँगा लेकिन पहले मुझे सामग्री के साथ आबादी वाले कोशिकाओं को प्राप्त करने की आवश्यकता है।ऑटोलायआउट
यदि मैं initWithFrame:
के साथ अपना फ्रेम सेट करता हूं तो मैं लेबल दिखा सकता हूं, हालांकि बाधाओं को लागू नहीं किया जा रहा है।
प्रश्न: लेबल और बाधाओं को प्रदर्शित होने से क्या रोक रहा है? मैं UITableViewCell के कार्यान्वयन में कुछ याद कर रहा हूं लेकिन मुझे नहीं पता कि यह क्या है।
माध्यमिक प्रश्न: क्या मैं
viewDidLoad
में प्रत्येक पुन: उपयोगकर्ता के लिए सही ढंग से UITableViewCell क्लास को पंजीकृत कर रहा हूं?
यह मुश्किल होने के रूप में देखने को मिल सकते हैं, लेकिन इंटरफ़ेस बिल्डर मुझे confuses, मैं सभी कोड में यह पूरा करना चाहते हैं।
static NSString * const kCellIDTitle = @"CellWithTitle";
static NSString * const kCellIDTitleMain = @"CellWithTitleMain";
@interface TVTCell : UITableViewCell
{
NSString *reuseID;
}
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *mainLabel;
@end
और TVTCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
reuseID = reuseIdentifier;
nameLabel = [[UILabel alloc] init];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
[nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
[nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:nameLabel];
mainLabel = [[UILabel alloc] init];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]];
[mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
[mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:mainLabel];
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
}
return self;
}
- (void)updateConstraints
{
[super updateConstraints];
NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel);
if (reuseID == kCellIDTitle) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
}
if (reuseID == kCellIDTitleMain) {
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainLabel]|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel][mainLabel]|"
options: NSLayoutFormatAlignAllLeft
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:nameLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:44.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:nameLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:1]];
}
}
क्षमा करें, कोड की टन
यहां कस्टम UITableViewCell TVTCell.h नामित के लिए कोड है। यहाँ मेरी UITableView के tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 3) {
TVTCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIDTitle forIndexPath:indexPath];
[[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];
return cell;
} else if (indexPath.row == 1 || indexPath.row == 4 || indexPath.row == 5) {
TVTCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIDTitleMain forIndexPath:indexPath];
[[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];
[[cell mainLabel] setText:[dataArray objectAtIndex:indexPath.row]];
return cell;
} else
{
UITableViewCell *badCell = [[UITableViewCell alloc] init];
NSLog(@"Warning! returning a cell that shouldnt be here");
badCell.textLabel.text = @"Warning!";
return badCell;
}
}
और अंत में, UITableView के viewDidLoad विधि:
- (void)viewDidLoad
{
[super viewDidLoad];
[[self tableView] registerClass:[TVTCell class] forCellReuseIdentifier:kCellIDTitle];
[[self tableView] registerClass:[TVTCell class] forCellReuseIdentifier:kCellIDTitleMain];
}
मुझे एक ही भ्रम मिला। आपके प्रश्न के लिए धन्यवाद। – stackFish