जब चयनित आप एक UITableViewCell पर एक ऊंचाई परिवर्तन चेतन कर सकते हैं?

वोट
361

मैं एक का उपयोग कर रहा UITableViewमेरे iPhone app में, और मैं लोगों की है कि एक समूह के हैं की एक सूची है। मुझे यह पसंद है, ताकि जब उपयोगकर्ता एक विशेष व्यक्ति पर क्लिक करता है (इस प्रकार सेल को चुनकर), सेल ऊंचाई में बढ़ता है कि व्यक्ति के गुणों को संपादित करने के कई यूआई नियंत्रण दिखाना होगा।

क्या यह संभव है?

20/01/2009 को 04:17
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


21 जवाब

वोट
833

मैं एक करने के लिए एक पक्ष प्रभाव के रूप में इस के लिए एक बहुत आसान समाधान नहीं मिला UITableViewमैं पर काम कर रहा था .....

एक चर है कि मूल ऊंचाई सामान्य रूप से रिपोर्ट के माध्यम से में सेल ऊंचाई स्टोर tableView: heightForRowAtIndexPath:है, तो जब आप एक ऊंचाई परिवर्तन चेतन करना चाहते हैं, बस चर का मान बदल सकते हैं और यह कहते हैं ...

[tableView beginUpdates];
[tableView endUpdates];

आप इसे एक पूर्ण पुनः लोड नहीं करता है लगता है, लेकिन के लिए पर्याप्त है जाएगा UITableViewपता करने के लिए यह कोशिकाओं पुनः बनाने का, सेल के लिए नए ऊंचाई मूल्य हथियाने है .... और लगता है क्या? यह आप के लिए परिवर्तन एनिमेट। मिठाई।

मैं अपने ब्लॉग ... पर एक अधिक विस्तृत विवरण और पूर्ण कोड नमूने चेतन UITableView सेल ऊंचाई बदलें

14/01/2010 को 12:42
का स्रोत उपयोगकर्ता

वोट
56

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

किसी पूर्णांक बनाएं वर्तमान चयनित सेल सूचकांक के लिए एक मूल्य धारण करने के लिए:

int currentSelection;

फिर:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = [indexPath row];
    selectedNumber = row;
    [tableView beginUpdates];
    [tableView endUpdates];
}

फिर:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([indexPath row] == currentSelection) {
        return  80;
    }
    else return 40;


}

cellForRowAtIndexPath: सेल के लिए सेल के प्रकार बदलने के लिए या यहां तक ​​कि लोड एक xib फाइल करने के लिए मैं आप में tableView समान परिवर्तन कर सकते हैं यकीन है।

इस तरह, currentSelection शुरू कर देंगे 0. पर आप समायोजन करने के लिए की जरूरत है अगर आप (सूचकांक 0 पर) सूची के पहले सेल डिफ़ॉल्ट रूप से चयनित देखो नहीं करना चाहता था होगा।

26/04/2011 को 00:25
का स्रोत उपयोगकर्ता

वोट
19

एक संपत्ति चयनित सेल का ट्रैक रखने में जोड़े

@property (nonatomic) int currentSelection;

(उदाहरण के लिए) में एक प्रहरी मूल्य पर सेट करें viewDidLoad, यह सुनिश्चित करना है कि UITableView'सामान्य' की स्थिति में शुरू होता है

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //sentinel
    self.currentSelection = -1;
}

में heightForRowAtIndexPathआप ऊंचाई सेट कर सकते हैं आप चयनित सेल के लिए चाहते हैं

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;
    if ([indexPath row] == self.currentSelection) {
        rowHeight = self.newCellHeight;
    } else rowHeight = 57.0f;
    return rowHeight;
}

में didSelectRowAtIndexPathआप, वर्तमान चयन को बचाने और एक गतिशील ऊंचाई बचाने यदि आवश्यक

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // do things with your cell here

        // set selection
        self.currentSelection = indexPath.row;
        // save height for full text label
        self.newCellHeight = cell.titleLbl.frame.size.height + cell.descriptionLbl.frame.size.height + 10;

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}

में didDeselectRowAtIndexPathचयन सूचकांक प्रहरी मूल्य वापस करने के लिए निर्धारित किया है और सेल वापस सामान्य फार्म के लिए चेतन

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {       
        // do things with your cell here

        // sentinel
        self.currentSelection = -1;

        // animate
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}
05/08/2013 को 17:02
का स्रोत उपयोगकर्ता

वोट
12

reloadData कोई एनीमेशन है, क्योंकि वहाँ कोई अच्छा है ...

यह मैं वर्तमान में क्या कोशिश कर रहा हूँ है:

NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

यह लगभग सही काम करता है। लगभग। मैं सेल की ऊंचाई बढ़ रही हूँ, और कभी कभी एक छोटी सी "हिचकी" सेल के रूप में तालिका दृश्य में है बदल दिया जाता है, जैसे कि तालिका दृश्य में कुछ स्क्रॉल स्थिति संरक्षित किया जा रहा है, नए सेल (जो प्रथम कक्ष है तालिका में) ने अपने ऑफसेट बहुत अधिक के साथ समाप्त होता है, और scrollview इसका स्थान बदलना चली जाती है।

07/05/2009 को 00:24
का स्रोत उपयोगकर्ता

वोट
10

मैं के साथ हल reloadRowsAtIndexPaths

मैं में बचाने didSelectRowAtIndexPathका चयन किया और फोन सेल के indexPath reloadRowsAtIndexPathsअंत में (आप तत्व की सूची के लिए NSMutableArray भेज सकते हैं आप पुनः लोड चाहते हैं)।

में heightForRowAtIndexPathआप देख सकते हैं कि indexPath सूची में है या expandIndexPath सेल की नहीं और ऊंचाई भेजें।

आप इस बुनियादी उदाहरण देख सकते हैं: https://github.com/ferminhg/iOS-Examples/tree/master/iOS-UITableView-Cell-Height-Change/celdascambiadetam यह एक सरल उपाय है।

मैं कोड का एक तरह जोड़ें यदि आपने मदद

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath
{
    if ([indexPath isEqual:_expandIndexPath])
        return 80;

    return 40;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [cell.textLabel setText:@"wopwop"];

    return cell;
}

#pragma mark -
#pragma mark Tableview Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *modifiedRows = [NSMutableArray array];
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
    _expandIndexPath = indexPath;
    [modifiedRows addObject:indexPath];

    // This will animate updating the row sizes
    [tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
14/11/2014 को 13:20
का स्रोत उपयोगकर्ता

वोट
10

मैं नहीं जानता कि क्या लगातार beginUpdates / endUpdates बुला के बारे में सभी इस सामग्री है, तो आप सिर्फ उपयोग कर सकते हैं -[UITableView reloadRowsAtIndexPaths:withAnimation:]यहाँ एक उदाहरण परियोजना है

02/08/2014 को 23:52
का स्रोत उपयोगकर्ता

वोट
2
BOOL flag;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    flag = !flag;
    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] 
                     withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES == flag ? 20 : 40;
}
15/08/2016 को 16:32
का स्रोत उपयोगकर्ता

वोट
2

बस मुझे जैसे किसी कस्टम सेल पर "अधिक विवरण" जोड़ने के लिए खोज के लिए एक नोट।

[tableView beginUpdates];
[tableView endUpdates];

एक उत्कृष्ट काम किया है, लेकिन करने के लिए 'फसल' सेल दृश्य भूल नहीं है। इंटरफ़ेस बिल्डर से अपने सेल का चयन करें -> सामग्री देखें -> संपत्ति इंस्पेक्टर से चुनें " क्लिप subview "

25/03/2015 को 20:08
का स्रोत उपयोगकर्ता

वोट
2

इस indexwise पंक्ति के विस्तार के लिए है का प्रयास करें:

@property (nonatomic) NSIndexPath *expandIndexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if ([indexPath isEqual:self.expandedIndexPath])
    return 100;

return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *modifiedRows = [NSMutableArray array];
if ([indexPath isEqual:self.expandIndexPath]) {
    [modifiedRows addObject:self.expandIndexPath];
    self.expandIndexPath = nil;
} else {
    if (self.expandedIndexPath)
        [modifiedRows addObject:self.expandIndexPath];

    self.expandIndexPath = indexPath;
    [modifiedRows addObject:indexPath];
}

// This will animate updating the row sizes
[tableView reloadRowsAtIndexPaths:modifiedRows withRowAnimation:UITableViewRowAnimationAutomatic];

// Preserve the deselection animation (if desired)
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewControllerCellReuseIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"I'm cell %ld:%ld", (long)indexPath.section, (long)indexPath.row];

return cell;
}
24/09/2014 को 06:41
का स्रोत उपयोगकर्ता

वोट
1

के बजाय beginUpdates()/ endUpdates(), की सिफारिश की कॉल अब है:

tableView.performBatchUpdates(nil, completion: nil)

एप्पल का कहना है, के बारे में beginUpdates / endUpdates: "performBatchUpdates (_ का उपयोग करें:। पूरा होने :) इस एक जब भी संभव हो के बजाय विधि"

देखें: https://developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates

19/11/2018 को 16:47
का स्रोत उपयोगकर्ता

वोट
1

यहाँ स्विफ्ट 3. के लिए सिमंस जवाब के संक्षिप्त संस्करण का भी सेल के चयन के लिए अनुमति देता है टॉगल

var cellIsSelected: IndexPath?


  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    cellIsSelected = cellIsSelected == indexPath ? nil : indexPath
    tableView.beginUpdates()
    tableView.endUpdates()
  }


  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if cellIsSelected == indexPath {
      return 250
    }
    return 65
  }
30/05/2017 को 23:36
का स्रोत उपयोगकर्ता

वोट
1

साइमन ली की जवाब की स्विफ्ट संस्करण।

// MARK: - Variables 
  var isCcBccSelected = false // To toggle Bcc.



    // MARK: UITableViewDelegate
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    // Hide the Bcc Text Field , until CC gets focused in didSelectRowAtIndexPath()
    if self.cellTypes[indexPath.row] == CellType.Bcc {
        if (isCcBccSelected) {
            return 44
        } else {
            return 0
        }
    }

    return 44.0
}

didSelectRowAtIndexPath में तब ()

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)

    // To Get the Focus of CC, so that we can expand Bcc
    if self.cellTypes[indexPath.row] == CellType.Cc {

        if let cell = tableView.cellForRowAtIndexPath(indexPath) as? RecipientTableViewCell {

            if cell.tag == 1 {
                cell.recipientTypeLabel.text = "Cc:"
                cell.recipientTextField.userInteractionEnabled = true
                cell.recipientTextField.becomeFirstResponder()

                isCcBccSelected = true

                tableView.beginUpdates()
                tableView.endUpdates()
            }
        }
    }
}
07/06/2016 को 15:30
का स्रोत उपयोगकर्ता

वोट
0

स्विफ्ट 4 और ऊपर

आप tableview के didselect पंक्ति प्रतिनिधि विधि में नीचे दिए गए कोड को जोड़ने

tableView.beginUpdates()
tableView.setNeedsLayout()
tableView.endUpdates()
23/11/2018 को 11:53
का स्रोत उपयोगकर्ता

वोट
0

इनपुट -

tableView.beginUpdates () tableView.endUpdates () इन कार्यों फोन नहीं होगा

समारोह tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}

लेकिन, यदि आप, कर tableView.reloadRows (कम: [selectedIndexPath IndexPath के रूप में!], के साथ: .none)

यह फोन करेगा समारोह tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {} इस समारोह।

28/03/2018 को 11:08
का स्रोत उपयोगकर्ता

वोट
0

की स्विफ्ट संस्करण साइमन ली की जवाब :

tableView.beginUpdates()
tableView.endUpdates()

ध्यान रखें कि आप ऊंचाई गुण को संशोधित करना चाहिए रखें पहले endUpdates()

08/02/2018 को 14:11
का स्रोत उपयोगकर्ता

वोट
0

हाँ, यह मुमकिन है।

UITableView एक प्रतिनिधि विधि है didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView animateWithDuration:.6
                          delay:0
         usingSpringWithDamping:UIViewAnimationOptionBeginFromCurrentState
          initialSpringVelocity:0
                        options:UIViewAnimationOptionBeginFromCurrentState animations:^{

                            cellindex = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
                            NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
                            [violatedTableView beginUpdates];
                            [violatedTableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
                            [violatedTableView endUpdates];
                        }
                     completion:^(BOOL finished) {
    }];
}

लेकिन आपके मामले में उपयोगकर्ता स्क्रॉल और एक अलग सेल का चयन करता है तो यू पिछले चयनित सेल हटना और वर्तमान में चयनित सेल का विस्तार करने की आवश्यकता है, तो reloadRowsAtIndexPaths:कॉल heightForRowAtIndexPath:तो तदनुसार संभाल।

01/08/2016 को 11:29
का स्रोत उपयोगकर्ता

वोट
0

iOS 7 और बाद में के बाद इस विधि की जाँच करें।

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

सुधार इस आईओएस 8. में हम तालिका दृश्य खुद की संपत्ति के रूप में सेट कर सकते हैं करने के लिए बनाया गया है।

04/03/2016 को 12:10
का स्रोत उपयोगकर्ता

वोट
0

मैं @ जोय के भयानक जवाब इस्तेमाल किया है, और यह ios 8.4 और XCode 7.1.1 के साथ पूरी तरह से काम किया।

मामले में आप अपने सेल टॉगल करने योग्य बनाने के लिए देख रहे हैं, मैं निम्नलिखित करने के लिए -tableViewDidSelect बदल दिया है:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//This is the bit I changed, so that if tapped once on the cell, 
//cell is expanded. If tapped again on the same cell, 
//cell is collapsed. 
    if (self.currentSelection==indexPath.row) {
        self.currentSelection = -1;
    }else{
        self.currentSelection = indexPath.row;
    }
        // animate
        [tableView beginUpdates];
        [tableView endUpdates];

}

मुझे आशा है कि इस के किसी भी आप की मदद की।

19/11/2015 को 21:00
का स्रोत उपयोगकर्ता

वोट
0

यहां कस्टम के अपने कोड है UITableViewउपवर्ग है, जो विस्तार UITextViewतालिका सेल में, पुन: लोड (और खो, कीबोर्ड) के बिना:

- (void)textViewDidChange:(UITextView *)textView {
    CGFloat textHeight = [textView sizeThatFits:CGSizeMake(self.width, MAXFLOAT)].height;
    // Check, if text height changed
    if (self.previousTextHeight != textHeight && self.previousTextHeight > 0) {
        [self beginUpdates];

        // Calculate difference in height
        CGFloat difference = textHeight - self.previousTextHeight;

        // Update currently editing cell's height
        CGRect editingCellFrame = self.editingCell.frame;
        editingCellFrame.size.height += difference;
        self.editingCell.frame = editingCellFrame;

        // Update UITableView contentSize
        self.contentSize = CGSizeMake(self.contentSize.width, self.contentSize.height + difference);

        // Scroll to bottom if cell is at the end of the table
        if (self.editingNoteInEndOfTable) {
            self.contentOffset = CGPointMake(self.contentOffset.x, self.contentOffset.y + difference);
        } else {
            // Update all next to editing cells
            NSInteger editingCellIndex = [self.visibleCells indexOfObject:self.editingCell];
            for (NSInteger i = editingCellIndex; i < self.visibleCells.count; i++) {
                UITableViewCell *cell = self.visibleCells[i];
                CGRect cellFrame = cell.frame;
                cellFrame.origin.y += difference;
                cell.frame = cellFrame;
            }
        }
        [self endUpdates];
    }
    self.previousTextHeight = textHeight;
}
10/11/2015 को 02:14
का स्रोत उपयोगकर्ता

वोट
-1

मैं सिर्फ एक छोटे से हैक के साथ इस समस्या का समाधान:

static int s_CellHeight = 30;
static int s_CellHeightEditing = 60;

- (void)onTimer {
    cellHeight++;
    [tableView reloadData];
    if (cellHeight < s_CellHeightEditing)
        heightAnimationTimer = [[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(onTimer) userInfo:nil repeats:NO] retain];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (isInEdit) {
            return cellHeight;
        }
        cellHeight = s_CellHeight;
        return s_CellHeight;
}

जब मैं सेल ऊंचाई मैं सेट का विस्तार करने की जरूरत है isInEdit = YESऔर विधि कॉल [self onTimer]और यह कोशिकाओं की वृद्धि एनिमेट जब तक यह s_CellHeightEditing मूल्य तक पहुंचने :-)

20/08/2009 को 21:18
का स्रोत उपयोगकर्ता

वोट
-1

चयनित पंक्ति के indexpath प्राप्त करें। तालिका से लोड करें। UITableViewDelegate की heightForRowAtIndexPath विधि में, सेट एक अलग ऊंचाई करने के लिए और दूसरों के लिए चयनित पंक्ति की ऊंचाई सामान्य पंक्ति की ऊँचाई वापसी

21/01/2009 को 16:13
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more