2010-07-05 24 views
6

में एकाधिक सेल स्टाइल प्रकारों और कस्टम कक्षों का उपयोग करके मुझे समस्याएं आ रही हैं कि UITableView के भीतर अलग-अलग सेल शैलियों के साथ-साथ कस्टम सेल को कैसे प्रदर्शित किया जाए। मैं समझता हूं कि कैसे एक साथ कोशिकाओं को स्थापित करना और एक मूल UITableView का निर्माण करना है, लेकिन केवल एक के भीतर सेल को "मिश्रण और मिलान" करने का तरीका नहीं है।UITableView

सबसे अच्छा उदाहरण मैं आपको जो हासिल करने की कोशिश कर रहा हूं उस पर आपको दिखा सकता हूं Tweetie 2 एप्लिकेशन के साथ है। Tweetie 2 profile

खंड के शीर्ष वहाँ तो यह वहाँ UITableViewCell के UITableViewCellStyleValue2 शैली सेट के साथ नीचे एक ब्लॉक पैरा है। इस प्रभाव को प्राप्त करने के बारे में मैं वास्तव में कैसे जाऊं? समय

उत्तर

8

की

धन्यवाद आगे मुख्य लेआउट एक समूहीकृत तालिका है। कोशिकाओं का प्रत्येक समूह एक तालिका खंड है। शीर्षतम सेल एक पारदर्शी पृष्ठभूमि के साथ सेट है।

इस काम को बनाने की कुंजी टेबलव्यू प्रतिनिधि में एक तार्किक संरचना है जो समझता है कि कौन सा सेल लेआउट अनुभाग और कौन सी पंक्ति में जाता है। एक स्विच स्टेटमेंट आमतौर पर सबसे आसान होता है हालांकि आप लेआउट को प्रतिबिंबित करने के लिए सरणी या शब्दकोश कॉन्फ़िगरेशन का उपयोग भी कर सकते हैं। सुविधाजनक तंत्र के रूप

switch (indexPath.section) { 
    case 0: 
     cell= //configure cell with transparent background 
     break; 
    case 1: 
     if (indexPath.row==0) { 
      cell = // configure cell for multiline 
     }else { 
      cell = // configure for UITableViewCellStyleValue2 
     } 
     break; 
    case 2: 
     // .. and so on for each section and cell 
     break; 
    default: 
     break; 
} 

इस लेआउट में, tableview एक तार्किक तालिका के रूप में कम किया जा रहा है (सूची संरचित डेटा की इकाइयों को दोहरा प्रदर्शित करता है जो) और अधिक:

तो, tableView:cellForRowAtIndexPath: में आप की तरह कुछ होता है एक लेआउट के प्रबंधन के लिए। टेबलव्यू के प्रबंधन का तर्क अधिक जटिल होना चाहिए और लेआउट की ज़रूरतों को प्रतिबिंबित करना होगा।

4

सबसे प्रत्यक्ष दृष्टिकोण -tableView:cellForRowAtIndexPath: का उपयोग indexPath.section और indexPath.row का उपयोग करने के लिए करना होगा ताकि यह निर्धारित किया जा सके कि किस प्रकार का सेल आकर्षित करना है। उदाहरण के लिए:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
    if (indexPath.row == 0) { 
     // return long text style cell 

    } else { 
     // return left/right label style cell 
    } 

    } else { 
    // return the 4-way button style cell 
    } 
} 

कितने कोशिकाओं आप प्रदान कर रहे हैं और आप कितने सेल शैलियों मिल गया है, तो आप फिर से उपयोग करने के लिए कोशिकाओं जो मामले में आप में से प्रत्येक शैली के लिए एक अलग सेल पहचानकर्ता का उपयोग करना चाहिए आवश्यकता हो सकती है पर निर्भर करता है सेल।

0

Tweetie के विभाजन सेल के रूप में एक ही प्रभाव प्राप्त करने के लिए, एक कस्टम सेल बना सकते हैं और एक सेगमेंट किए गए नियंत्रण जोड़ सकते हैं और शीर्षक बना सकते हैं और विस्तार लेबल alt text

1

यह करने के लिए एक सुंदर तरीका है:

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

    /* 
     Call a function to create all custom cells. 
     Send the tableview and the indexPath to this function. 
     So, your code will be clean and easy to read an maintenance =D 
     DON'T forget to change the height of each cell 
    */ 
    if (indexPath.row < 3) 
     return [self createACustomCell1:tableView indexPath:indexPath]; 
    else 
     return [self createACustomCell2:tableView indexPath:indexPath]; 

} 


//************* 
// Create CUSTOM CELL 2 
//************* 
-(UITableViewCell *)createACustomCell1:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{ 
    static NSString *CUSTOMCELL_1 = @"CUSTOMCELL_1"; 

    CustomCell_1 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1]; 
    if (!cell){ 
    [anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_1 
              bundle:nil] forCellReuseIdentifier:CUSTOMCELL_1]; 
     cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1]; 
    } 

    // Cell customization above 
    return cell; 
} 


//************* 
// Create CUSTOM CELL 2 
//************* 
-(UITableViewCell *)createACustomCell2:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{ 
    static NSString *CUSTOMCELL_2 = @"CUSTOMCELL_2"; 

    CustomCell_2 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2]; 
    if (!cell){ 
    [anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_2 
              bundle:nil] forCellReuseIdentifier:CUSTOMCELL_2]; 
     cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2]; 
    } 

    // Cell customization above 


    return cell; 
}