Good time colleagues!

Faced such a task. It is necessary to display information in the UITableView in two custom UITableViewCell. upper title as if the title, lower text with arbitrary height. If you click on the title you need to go to the user profile, if you click on the text, go to the full text of the article. The example of the implementation that I need is well executed in the VKontakte application for iphone, in the group section. I can't get the data into two UITableViewCell in turn.

https://www.dropbox.com/s/g1g6btk4zudrhr1/screen.pdf

In the attached file, I drew a prototype of how a table view should look on a mobile phone.

If anyone came across or knows how to do this, please help.

  • If I understand you correctly - you need to use custom cells. Attach UITapGesture to the labels in which you output information or, even more simply, make invisible buttons on the background of text blocks and use them to animate the transition to the corresponding screen. - AlexThumb
  • The author also writes that he needs to display information in two custom cells! - Stanislav Pankevich
  • @huffman, this is exactly what you should have exactly two different cells - that is, do you always have them in pairs? If so, why don't you use one cell that contains two of your sub-cells? - Stanislav Pankevich
  • Your pdf-illustration makes me think that you only have one kind of cell, it’s just a composite cell and consists of two sub-subsections. This is true? Does it happen that cells of the same type, for example, cells of the "multiline text" type, go in a row, without an author, in the gap between them? - Stanislav Pankevich

2 answers 2

After discussing this issue with its author in Skype, we found out that both types of cells actually belong to the same entity (the user and the content associated with it) and decided that it was necessary to create only one type of cell in which the content The current two cell types will come in the form of two subviews — one subview for the cell header (user information), the other for the content (user message).

The main problem was hidden in the fact that due to the presence of two types of cells, the author, relying on the string of the form

EXHeaderTableCell *headerCell = [tableView dequeueReusableCellWithIdentifier:HeaderCellIdentifier forIndexPath:indexPath]; 

received a wrong access to the index in the array of Reusable cells, since the indexPath in most cases pointed to the cell of the opposite type.

  • Stanislav, thanks for helping me with my question. - huffman
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellPersonIdentifier = @"CellPersonIdentifier"; static NSString *CellDataIdentifier = @"CellDataIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:(indexPath.row % 2 == 0 ? CellPersonIdentifier : CellDataIdentifier)]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:(indexPath.row % 2 == 0 ? @"CellPerson" : @"CellData") owner:self options:nil]; cell = [nib objectAtIndex:0]; } // далее заполняешь ячейки данными } 
  • one
    @ruman, in order not to duplicate your answer, I will just tell you that your solution with (indexPath.row% 2 == 0) is rather rough and immediately assumes that the cells are strictly pairs. Add to your answer a check for the type of data that is being asked for by the corresponding index and already on the basis of the data type determine the class of the custom cell. I in such cases do typedef enum with transfer of possible data types. By "data type" I mean one of two possible variants of "content" for two kinds of cells. - Stanislav Pankevich
  • Now I can’t check (I wrote the code from the knee)), but in my opinion, you can just do a type conversion: if (indexPath.row% 2 == 0) ((CustomPersonCell *) cell) .personTitle ... else (((CustomDataCell *) cell) .dataDescription .... I agree, roughly. It is assumed that the cells always go in pairs. - NSLeader
  • In general, that's right. Let's see how the author of the question clarifies his task. Plus your answer. - Stanislav Pankevich
  • I tried to do the following. In the storyboard on the screen with a UITableView, I added two UITableViewCell with different identifiers and arranged the necessary controls. The code is about the same as in the example. But as a result, it turns out that all the cells are filled with a cell that is located in the decoder on the UITableView at the very top. I can’t give the source code as it didn’t fit in by the number of characters - ( - huffman
  • screenshot of the location of UITableViewCell and controls: dropbox.com/s/91wh5p6s3z68ako/tableview.tiff screen shot how it works: dropbox.com/s/yw9ook50k5s64q3/tableview2.tiff - huffman