2012-03-16 28 views
13

मेरे पास UITableViewCell के साथ एक साधारण समस्या है। मैं जो चाहता हूं वह किसी चयनित सेल के टेक्स्ट रंग को बदलना है। मेरी cellForRowAtIndexPath विधि में, मैं सेट:UITableView सेल टेक्स्ट लेबल रंग

cell.textLabel.textColor = [UIColor whiteColor]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.highlightedTextColor = [UIColor orangeColor]; 

तो selectionStyleUITableViewCellSelectionStyleNone है, highlightedTextColor नहीं बदलेगा। तो मैं इन दोनों तरीकों का उपयोग पाठ रंग सेट करने के लिए:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];  
    return indexPath; 
} 
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];  
    return indexPath; 
} 

यह काम करता है, लेकिन tableview स्क्रॉल जब, रंग वापस बदल जाता है।

+0

एनएसआईएनएक्सएक्सपीथ * urIndexPath घोषित करने के बारे में क्या; आपके। एच में फिर आपके didSelecteRowAtIndexPath विधि में, चयनित सेल के इंडेक्सपैथ को urIndexPath पर पास करें, फिर UI को अपडेट करने के लिए डेटा को पुनः लोड करें, अपने सेलफॉररएट इंडेक्सपैथ में, कुछ ऐसा कहें, अगर urIndexPath == indexPath {नारंगी रंग सेट करें; } else {डिफ़ॉल्ट रूप से जो भी रंग डिफ़ॉल्ट है} – janusbalatbat

+0

@Akhildas जब आप तालिका को स्क्रॉल करते हैं, तो सेलफोररोएटइंडेक्स देखें: विधि हमेशा कॉल की जाएगी। लेकिन आपके पास अन्य विधियां नहीं हैं। बेहतर है कि आप इसे "सेलफोररोएट इंडेक्स:" विधि के अंदर "WillSelectRowAtIndexPath" विधि को कॉल कर सकते हैं काम करता है –

उत्तर

19

अगर (सेल == नहीं के बराबर) की जाँच

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
}  

और

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor]; 

} 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor]; 

} 

धन्यवाद में रंग निर्धारित करके समाधान समझे

14

आपको बस इतना करना है कि अगर (सेल == शून्य)

वह ठीक से काम करेंगे

cell.textLabel.textColor = [UIColor whiteColor]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.highlightedTextColor = [UIColor orangeColor]; 

के अंदर है।

0

सेट एक वैश्विक: int m_selectedCell;

में इसे संशोधित
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

और

बस सब है कि

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

में

if(m_selectedCell == indexPath.row){ 
... 
... 
}else{ 
} 

जोड़ने!

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