2009-12-13 15 views
16

नहीं बदल रहा है, जब मैं तालिकादृश्य पॉप्युलेट किया गया है, तो निम्न कोड का उपयोग करके UITableViewCell के फ़ॉन्ट को कस्टमाइज़ करने का प्रयास कर रहा हूं।UITableViewCell फ़ॉन्ट

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
    } 

    // Set up the cell 
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
    NSString *temp = [[NSString alloc] initWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; 
    cell.textLabel.text = temp; 
    [[cell textLabel] setTextColor:[UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1]]; 
    [[cell textLabel] setFont:[UIFont systemFontOfSize:12.0]]; 

    return cell; 
} 

मुझे के जीवन मैं नहीं जानता कि क्यों यह फॉन्ट नहीं बदलेगा के लिए! इसके अलावा उपर्युक्त कोड ठीक काम करता है यदि सेल कोड में cell.textLabel.text = @"TEST";

कोई सुझाव है तो मैं कड़ी मेहनत करता हूं? धन्यवाद!

उत्तर

29

सबसे पहले, आपको अपने सेल को ऑटोोरिज़ करना चाहिए। आप वर्तमान में पागल की तरह स्मृति लीक कर रहे हैं।

दूसरा, आपको फ़ॉन्ट को tableView:willDisplayCellForRow:atIndexPath: में अपडेट करना चाहिए। यदि आप मानक तालिका दृश्य का उपयोग कर रहे हैं, तो यह आपके सेल (यादृच्छिक समय पर) में परिवर्तन करेगा और आपको डेटा स्रोत विधि के बजाय tableView:willDisplayCellForRow:atIndexPath: में फ़ॉन्ट परिवर्तन, पृष्ठभूमि रंग इत्यादि जैसी चीजें करने की आवश्यकता होगी।

भी इस सूत्र देखें: What is -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?

+0

आप तरीकों जहां मैं इसे बदलना चाहिए पर विस्तृत कृपया सकते में इन संशोधनों को करना है? धन्यवाद! –

+1

कभी नहीं, बस इसे मिला। –

4

बस आप कोड को निम्न से कोशिश कर सकते हैं।


-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *cellString = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString]; 
    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellString]; 
    } 
    cell.textLabel.text = values; 
    cell.textLabel.textColor = [UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1]; 
    cell.textLabel.font = [UIFont systemFontOfSize:12.0]; 
    return cell; 
}
+0

सेल.फॉन्ट अब – barfoon

+0

को हटा दिया गया है cell.textLabel.font या cell.detailTextLabel.font – jfalexvijay

+1

का उपयोग करने का प्रयास करें, तो आपको जवाब अपडेट करना चाहिए – barfoon

8

@Jason कोको:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
} 
3

प्रयास करें,

cell.textLabel.font = [UIFont systemFontOfSize:12.0]; 
cell.detailTextLabel.font = [UIFont systemFontOfSize:10.0]; 
-2

बहुत ही सरल, `

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 

    cell.textLabel.textColor = [UIColor grayColor]; //redColor greenColor etc. 
} 
1

willDisplayCell: forRowAtIndexPath: सेल की पृष्ठभूमि को संशोधित करने के लिए इस्तेमाल किया जा सकता रंग, सामग्री दृश्य, आदि को छोड़कर पाठलेबल और detailtextlabel

इसका हमेशा एक अच्छा अभ्यास की पृष्ठभूमि रंग संपत्ति उपरोक्त विधि