2010-08-16 12 views
25

ए (जब सेल नव निर्मित है):मुझे सेल.contentView में सबव्यूव्यू कैसे जोड़ना चाहिए?

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

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

     CGRect frame = CGRectMake(0, 0, 160, 50); 
     UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
     label.textAlignment = UITextAlignmentRight; 
     label.text = @"9:00am"; 
     [cell.contentView addSubview:label]; 
     [label release]; 
    } 

    return cell; 
} 

या बी (हर बार जब सेल पाया जाता है):

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

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

    CGRect frame = CGRectMake(0, 0, 160, 50); 
    UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
    label.textAlignment = UITextAlignmentRight; 
    label.text = @"9:00am"; 
    [cell.contentView addSubview:label]; 
    [label release]; 

    return cell; 
} 

ए या बी? धन्यवाद!

अद्यतन समाधान (जवाब के लिए धन्यवाद):

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

    static NSString *CellIdentifier = @"Cell";  
    UILabel *label; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     CGRect frame = CGRectMake(0, 0, 160, 50); 
     label = [[UILabel alloc] initWithFrame:frame]; 
     label.textAlignment = UITextAlignmentRight; 
     label.tag = 1; 
     [cell.contentView addSubview:label]; 
     [label release]; 
    } else { 
     label = (UILabel *) [cell viewWithTag:1]; 
    } 

    label.text = [NSString stringWithFormat:@"%d", [indexPath row]]; 

    return cell; 
} 

उत्तर

11

यह प्रदर्शन के बारे में सब कुछ है। ए के साथ, आप बी के साथ अपने सभी सबव्यूज़ के साथ सेल का पुन: उपयोग करते हैं, आप केवल कच्चे सेल का पुन: उपयोग करते हैं और प्रत्येक पुनरावृत्ति का एक नया सबव्यूव जोड़ते हैं, जो आईएमएचओ एक पुन: प्रदर्शन के रूप में उतना अच्छा नहीं है।

मैं कहता हूँ या तो एक UITableView उपवर्ग या उपयोग समाधान ए

+0

प्रदर्शन इसके अलावा, करता है बी कारण स्मृति रिसाव बनाने? – ohho

+0

@ohho, ऐसा मत सोचो। सब कुछ ठीक दिखता है। –

+3

क्या बी सेल में लेबल जोड़ना नहीं चाहेगा? मुझे लगता है कि कुछ स्क्रॉलिंग के बाद, आप अपने सबव्यूज़ में एकाधिक लेबल रखने वाले एकाधिक सेल्स के साथ समाप्त हो जाएंगे। कृपया मुझे सुधारें अगर मैं गलत हूं। – Mike

11

आप केवल जब आप एक के रूप में सेल बनाने के उप विचारों जोड़ना चाहिए, हालांकि लेबल आदि के रूप में हर बार को मान निर्दिष्ट बी

यह समाधान स्वाभाविक रूप से बाहर निकल जाएगा यदि आप UITableViewCell का अपना उप-वर्ग बनाते हैं जो इसके स्वयं के उप दृश्य जोड़ता है।

ऐसा कुछ।

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

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

     CGRect frame = CGRectMake(0, 0, 160, 50); 
     UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
     label.textAlignment = UITextAlignmentRight; 
     [cell.contentView addSubview:label]; 
     [label release]; 
    } 

    // Get a reference to the label here 

    label.text = @"9:00am"; 

    return cell; 
} 

इस तरह आप केवल उप विचारों एक बार आवंटन के प्रदर्शन लाभ मिलता है और आप बस के रूप में की जरूरत subview पर उचित गुण सेट कर सकते हैं।

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

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