में स्थैतिक कक्ष सेट करना मैं प्रोग्रामेटिक रूप से उद्देश्य सी में एक टेबलव्यू बना रहा हूं। मैं सेल को प्रोग्रामिक रूप से स्थैतिक कैसे बना सकता हूं?uitableview प्रोग्रामेटिक रूप से
धन्यवाद
में स्थैतिक कक्ष सेट करना मैं प्रोग्रामेटिक रूप से उद्देश्य सी में एक टेबलव्यू बना रहा हूं। मैं सेल को प्रोग्रामिक रूप से स्थैतिक कैसे बना सकता हूं?uitableview प्रोग्रामेटिक रूप से
धन्यवाद
हर एक आप इसे मिल जाएगा के लिए एक अलग सेल पहचानकर्ता का उपयोग करके। आप इस तरह कुछ उपयोग कर सकते हैं:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"s%i-r%i", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
//you can customize your cell here because it will be used just for one row.
}
return cell;
}
कोशिकाओं को स्थैतिक प्रोग्रामेटिक रूप से बनाना वास्तव में समझ में नहीं आता है। स्टेटिक सेल मूल रूप से इंटरफेस बिल्डर के लिए हैं और पूरे टेबल व्यू को स्थिर होने की आवश्यकता है। वे आपको UILables, UITextFields, UIImageViews, आदि को सीधे सेल में खींचने की अनुमति देते हैं और यह दिखाता है कि ऐप चलाने पर एक्सकोड में कैसा दिखता है।
हालांकि, आपकी कोशिकाएं बाहरी डेटा स्रोत और हार्डकोडिंग सब कुछ का उपयोग करके प्रोग्रामिक रूप से "स्थैतिक" हो सकती हैं, जो आमतौर पर गन्दा और आम तौर पर एक गरीब विचार होने जा रही है।
मैं एक .xib के साथ एक नया UITableViewController बनाने का सुझाव देता हूं और यदि आप "स्थैतिक" कोशिकाओं को चाहते हैं तो इसे वहां से अनुकूलित कर सकते हैं। अन्यथा, बस अपने सभी मूल्यों को हार्डकोड करें और यह मूल रूप से वही बात है, लेकिन संभवतः खराब डिज़ाइन है यदि इसे टाला जा सके।
आप इसे पुराने तरीके से भी कर सकते हैं और केवल NSIndexPath
के आधार पर सेल को जिस तरह से चाहते हैं, यह स्टेटिक सेल टीवीसी और नियमित तालिका दृश्यों के साथ काम करता है (अनुभागों और पंक्तियों की उचित संख्या को वापस न भूलें उनके डेटा स्रोत के तरीकों में):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch indexPath.row {
case 0:
// First cell, setup the way you want
case 1:
// First cell, setup the way you want
}
// return the customized cell
return cell;
}
मैं तुम्हें ऐसे ही एक सेटिंग स्क्रीन या कुछ और के लिए उदाहरण के लिए कोशिकाओं संरचना बनाने के लिए चाहते हैं और आप शायद सामग्री कुछ कोशिकाओं को संशोधित करने के नहीं, बल्कि उनकी संख्या या वर्गों संरचना आप को ओवरलोड कर सकते हैं बस जरूरत है इस तरह के UITableViewController उपclass की विधि:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// Configure the cell...
if ([aCell.reuseIdentifier isEqualToString:@"someIdentifier"]){
//some configuration block
}
else if ([aCell.reuseIdentifier isEqualToString:@"someOtherIdentifier"]) {
//other configuration block
}
return aCell;
}
लेकिन आप इसे थोड़ा और कोड के साथ बेहतर तरीके से बना सकते हैं;
1) अपने .m फ़ाइल की शुरुआत में जोड़ने typedef:
typedef void(^IDPCellConfigurationBlock)(UITableViewCell *aCell);
2) अपने TablviewControllerSubclass विस्तार करने के लिए cellConfigurations संपत्ति जोड़ें:
@interface IPDSettingsTableViewController()
@property (nonatomic, strong) NSDictionary *cellConfigurations;
@property (nonatomic) id dataModel;
@end
3) TableviewController उपवर्ग के अपने स्थिर कोशिकाओं को संशोधित स्टोरीबोर्ड या xib में और प्रत्येक सेल के लिए अद्वितीय सेलReuseIdentifier जोड़ें जिसे आप प्रोग्रामेटिक रूप से संशोधित करना चाहते हैं
4) अपने viewDidLoad विधि सेटअप cellsConfiguration ब्लॉक में:
- (void)viewDidLoad
{
[super viewDidLoad];
[self SetupCellsConfigurationBlocks];
}
- (void)SetupCellsConfigurationBlocks
{
//Store configurations code for each cell reuse identifier
NSMutableDictionary *cellsConfigurationBlocks = [NSMutableDictionary new];
//store cells configurations for a different cells identifiers
cellsConfigurationBlocks[@"someCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.backgroundColor = [UIColor orangeColor];
};
cellsConfigurationBlocks[@"otherCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.imageView.image = [UIImage imageNamed:@"some image name"];
};
//use waek reference to self to avoid memory leaks
__weak typeof (self) weakSelf = self;
cellsConfigurationBlocks[@"nextCellIdentifier"] = ^(UITableViewCell *aCell){
//You can even use your data model to configure cell
aCell.textLabel.textColor = [[weakSelf.dataModel someProperty] isEqual:@YES] ? [UIColor purpleColor] : [UIColor yellowColor];
aCell.textLabel.text = [weakSelf.dataModel someOtherProperty];
};
weakSelf.cellConfigurations = [cellsConfigurationBlocks copy];
}
5) ओवरलोड tableView: इस तरह cellForRowAtIndexPath विधि:
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// configure cell
[self configureCell:aCell withConfigurationBlock:self.cellConfigurations[aCell.reuseIdentifier]];
return aCell;
}
- (void)configureCell:(UITableViewCell *)aCell withConfigurationBlock:(IDPCellConfigurationBlock)configureCellBlock
{
if (configureCellBlock){
configureCellBlock(aCell);
}
}
यह एक के रूप में उपयोग करने के लिए एक सरल तालिका का निर्माण करना चाहते करने के लिए बहुत आम है मेनू या फॉर्म, लेकिन डेटासोर्स और प्रतिनिधि कॉलबैक के साथ निर्मित एपीआई का उपयोग करना लिखना या बनाए रखना आसान नहीं है। आपको कुछ कक्षों को गतिशील रूप से जोड़ने/निकालने/अपडेट करने की आवश्यकता हो सकती है, इसलिए स्टोरीबोर्ड का उपयोग करके स्वयं काम नहीं करेगा।
मैंने प्रोग्रामिंग रूप से छोटी टेबल बनाने के लिए MEDeclarativeTable को एक साथ रखा।यह UITableView
के लिए डेटास्रोत और प्रतिनिधि प्रदान करता है। हम एक एपीआई के साथ समाप्त होते हैं जहां हम डेटा स्रोत और प्रतिनिधि विधियों को लागू करने के बजाय अनुभागों और पंक्तियों के उदाहरण प्रदान करते हैं।