我試圖創建一個類似的截圖應用程序:iOS:Xcode - 如何顯示單元格下的內容?
它看起來像自來水細胞透露下內容的表視圖。有誰知道如何做到這一點。我設法創建了一個單元格,其中包含我想要顯示的內容,並在加載表格時限制單元格的高度。輕拍時,高度增加。但這不是我想要的。此外,要顯示的內容可以是任何高度,所以我需要相應地調整單元格大小。
也許還有另一種做法,我試圖實現?
任何輸入將不勝感激。
編輯: 好,我得到這個工作的程度,但也有一定的問題,這一點: 1)某些行的距離展開和摺疊單元前面行 2)顯示數據內容改變,當它應該只是顯示/隱藏
我的代碼:
#import "ViewController.h"
#import "Data.h"
@interface ViewController()
@end
@implementation ViewController {
NSArray *data;
NSInteger selectedIndex;
NSIndexPath *selectedCellIndexPath;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
selectedIndex = -1;
self.navigationItem.title = @"Test App";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
// Create Data array
Data *data1 = [Data new];
data1.text = @"Abhijit ipsum dolor sit amet, consectetur adipiscing elit.";
data1.translation = @"AbhijitDE ipsum dolor sit amet, consectetur adipiscing elit.";
Data *data2 = [Data new];
data2.text = @"Anurag ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data2.translation = @"AnuragDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Data *data3 = [Data new];
data3.text = @"Chetan ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data3.translation = @"ChetanDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Data *data4 = [Data new];
data4.text = @"Eric ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data4.translation = @"EricDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Data *data5 = [Data new];
data5.text = @"Neha ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsumiraj dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data5.translation = @"NehaDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Data *data6 = [Data new];
data6.text = @"Nilesh ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data6.translation = @"NileshDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Data *data7 = [Data new];
data7.text = @"Niraj ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsumiraj dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data7.translation = @"NirajDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
data = [NSArray arrayWithObjects:data1, data2, data3, data4, data5, data6, data7, nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return data.count;
}
// This just a convenience function to get the height of the label based on the comment text
-(CGFloat)getLabelHeightForIndex:(NSString *)string
{
CGSize maximumSize = CGSizeMake(320, 10000);
CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
return labelHeightSize.height + 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *mainLabel, *secondLabel;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// First label
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.00)];
mainLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
mainLabel.textColor = [UIColor blackColor];
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.numberOfLines = 0;
mainLabel.lineBreakMode = NSLineBreakByWordWrapping;
// mainLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// mainLabel.clipsToBounds = YES;
[cell.contentView addSubview:mainLabel];
// Second label
secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 10.0, 320.0, 36.00)];
secondLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f];
secondLabel.textColor = [UIColor whiteColor];
secondLabel.backgroundColor = [UIColor grayColor];
secondLabel.numberOfLines = 0;
secondLabel.lineBreakMode = NSLineBreakByWordWrapping;
// secondLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// secondLabel.clipsToBounds = YES;
[cell.contentView addSubview:secondLabel];
}
// Get data for the current row
Data *dataContent = [data objectAtIndex:indexPath.row];
CGFloat dataTextHeight = [self getLabelHeightForIndex:dataContent.text];
mainLabel.frame = CGRectMake(0.0, 0.0, 320.0, dataTextHeight);
mainLabel.text = dataContent.text;
CGFloat dataTranslationHeight = [self getLabelHeightForIndex:dataContent.translation];
secondLabel.frame = CGRectMake(0.0, dataTextHeight + 10.00, 320.0, dataTranslationHeight);
secondLabel.text = dataContent.translation;
if(selectedIndex == indexPath.row)
{
secondLabel.hidden = YES;
}
else
{
secondLabel.hidden = YES;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get data for the current row
Data *dataContent = [data objectAtIndex:indexPath.row];
CGFloat dataTextHeight = [self getLabelHeightForIndex:dataContent.text];
CGFloat dataTranslationHeight = [self getLabelHeightForIndex:dataContent.translation];
if(selectedIndex == indexPath.row)
{
return (dataTextHeight + dataTranslationHeight) + 10;
}
else
{
return dataTextHeight;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// An expanded cell is clicked, minimize it
if(selectedIndex == indexPath.row)
{
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
// If any previous cell is expanded, minimize it
if(selectedIndex >= 0)
{
NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
}
// Expand the currently expanded cell
selectedIndex = indexPath.row;
NSIndexPath *currentPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:currentPath] withRowAnimation:UITableViewRowAnimationFade];
}
@end
什麼樣的「兜底」你要幹什麼?覆蓋視圖消失,顯示的項目滑入或? – bobnoble
好吧...基本上我編碼印度本地語言翻譯應用程序...基本上需要在表格視圖中顯示英語語句...點擊任何行應滑動單元格下方的翻譯...竊聽它再次應該將翻譯向後滑動...每行和翻譯的內容可以是任意長度,這意味着行的大小和翻譯應該是動態的... –