2012-06-16 20 views
5

मैंने एक टेबल जोड़ा है और इसमें एक तालिका दृश्य कक्ष खींच लिया है। उपयोगिता पैनल में , मैंने शैली को उपशीर्षक में बदल दिया। मैं भी कोड में इसे बदलने की कोशिश की है:आईओएस - UITableViewCell टेक्स्ट संरेखण

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.textAlignment = UITextAlignmentCenter; 
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 

    cell.textLabel.text = [myArray objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [myArray2 objectAtIndex:indexPath.row]; 

    return cell; 

केंद्र संरेखण काम नहीं करता है!

मैंने वर्कअराउंड करने के लिए सेल पर एक लेबल ऑब्जेक्ट जोड़ने का प्रयास किया है। लेकिन मुझे नहीं पता कि इसे कैसे पहुंचाया जाए। भले ही मैं इसे करने के लिए एक आउटलेट सौंपा, यह काम नहीं होगा:

cell.labelForCell.... 

मुझे क्या करना चाहिए? सेल या कुछ लेबल जोड़ने के बिना, मैं इसे सामान्य तरीके से कैसे काम करता हूं, इस पर कोई सुझाव?

+0

बस एक fyi, NSTextAlignmentCenter का उपयोग करें। UITextAlignmentCenter बहिष्कृत है। – Viper

उत्तर

18

UITableViewCellStyleSubtitle पाठ संरेखण के लिए नहीं बदला जा सकता है आप एक लेबल डाल दिया और इसे करने के लिए अपने संरेखण जोड़ने के लिए, है कि आप इस कोड

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UILabel *myLabel; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

     myLabel = [[UILabel alloc] initWithFrame:Your_Frame]; 
     //Add a tag to it in order to find it later 
     myLabel.tag = 111; 
     //Align it 
     myLabel.textAlignment= UITextAlignmentCenter; 

     [cell.contentView addSubview:myLabel]; 
    } 

    myLabel = (UILabel*)[cell.contentView viewWithTag:111]; 
    //Add text to it 
    myLabel.text = [myArray objectAtIndex:indexPath.row]; 

    return cell; 
} 
+0

धन्यवाद, अच्छा थोड़ा संदर्भ। – Morkrom

+0

कृपया ध्यान दें कि 'UITextAlignment' को IOS6 के बाद बहिष्कृत किया गया है। इसके बजाय 'NSTextAlignment' का उपयोग करें। –

0

इस्तेमाल कर सकते हैं मुझे लगता है कि कारण यह है कि है ऐसा करने के लिए करना होगा: पाठ लेबल की चौड़ाई टेक्स्ट लम्बाई पर निर्भर करती है, यदि पाठ सिग्नल लाइन में सभी को दिखाने के लिए बहुत लंबा है, और आप पहले ही लाइन ब्रेक मोड सेट कर चुके हैं और लाइनों की संख्या 0 पर सेट कर चुके हैं, तो आप पाएंगे कि टेक्स्ट संरेखण होगा काम।

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"identifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (nil == cell) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease]; 
     cell.textLabel.textAlignment = UITextAlignmentLeft; 
     cell.textLabel.textColor = [UIColor redColor]; 
     cell.detailTextLabel.textColor = [UIColor greenColor]; 
     cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 
     cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation; 
     cell.textLabel.numberOfLines = 0; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"You can initialize a very long string for the textLabel, or you can set the font to be a large number to make sure that the text cann't be shown in a singal line totally:%d", indexPath.row]; 
    return cell; 

}

+0

नहीं, पाठ बहुत छोटा था। मैं इसे संख्याओं के साथ परीक्षण कर रहा था। – Milad

1

उपशीर्षक शैली के साथ समस्या यह है कि यह करता है एक [self.textLabel sizeToFit] जब यह पता देता है। जब आप एक कंटेनर में केंद्रित होते हैं जो सामग्री का सही आकार है, तो कुछ भी नहीं बदलता है।

इसे आजमाएं। UITableViewCell का एक उपवर्ग में, अपने textAlignment निर्धारित करते हैं, और अपने layoutSubviews कोड के रूप में इस का उपयोग करें:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    { 
     CGRect frame = self.textLabel.frame; 
     frame.size.width = CGRectGetWidth(self.frame); 
     frame.origin.x = 0.0; 
     self.textLabel.frame = frame; 
    } 

    { 
     CGRect frame = self.detailTextLabel.frame; 
     frame.size.width = CGRectGetWidth(self.frame); 
     frame.origin.x = 0.0; 
     self.detailTextLabel.frame = frame; 
    } 
} 

यह पाठलेबल के फ्रेम पूरी चौड़ाई में आता है, और इस तरह केंद्रित प्रभाव ध्यान देने योग्य हो सकते हैं।

नोट: चूंकि यह लेआउट सबव्यूव्स ओवरराइड करता है, वहां एक प्रदर्शन लागत होती है क्योंकि इसे अक्सर कहा जाएगा।

+0

अच्छा सरल समाधान – Drakes

0

मुझे लगता है कि फ्रेम समायोजित करना काम करेगा। मेरे पास UITableViewCellStyleValue2 की शैली थी, लेकिन यदि मेरे पास टेक्स्ट लेबल में थोड़ा लंबा टेक्स्ट है, तो यह पूंछ और टेक्स्ट पर छोटा हो जाता है। संरेखण यहां काम नहीं करता है, इसलिए चौड़ाई टेक्स्ट लेबल को बढ़ाने का विचार किया जाता है।

-(void)layoutSubviews{ 
     [super layoutSubviews]; 

     if ([self.reuseIdentifier isEqualToString:@"FooterCell"]) { 
      CGRect aTframe = self.textLabel.frame; 
      aTframe.size.width += 40; 
      self.textLabel.frame = aTframe; 

      CGRect adTframe = self.detailTextLabel.frame; 
      adTframe.origin.x += 70; 
      self.detailTextLabel.frame = adTframe;   
     } 
    } 
0

यह UITableViewCell में पाठलेबल और detailTextLabel के लिए फ्रेम को बदलने का अधिकार cellForRowAtIndexPath में असंभव है: विधि।

आप सही डिफ़ॉल्ट layoutSubviews के बाद ज्यामिति को बदलने के लिए शून्य देरी से

performSelector:withObject:afterDelay: 

का उपयोग कर एक छोटा सा हैक लागू कर सकते हैं आप अपने सेल उपवर्ग के लिए नहीं करना चाहते हैं:

[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0]; 

विवरण देखें here

1

हाय कृपया अपने कोड के बजाय निम्नलिखित जोड़ें,

cell.textLabel.textAlignment = UITextAlignmentCenter; 
cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 

परिवर्तन

को
cell.textLabel.textAlignment = NSTextAlignmentCenter; 
cell.detailTextLabel.textAlignment = NSTextAlignmentCenter; 

यह ठीक काम करेंगे।

+4

क्षमा करें, लेकिन यह उत्तर गलत है। – aqua

+0

ठीक है, अगर सही मतलब है तो मेरा जवाब स्वीकार करें। –

संबंधित मुद्दे