自动调整TableView单元格

时间:2016-05-03 11:02:24

标签: ios uitableview autoresize

我创建了一个自定义TableView Cell,其中包含三个Label(一个用于标题,一个用于简短描述,最后一个用于长描述)。我希望它们被动态排列,但问题是第三个label,它无法动态排列高度。

Table View Custom Cell

Simulator Image which is not showing all the Dynamic text content

CompaniesTableViewController.h: -

#import <UIKit/UIKit.h>

@interface CompaniesTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>

@end

CompaniesTableViewController.m: -

#import "CompaniesTableViewController.h"
#import "TableViewCell.h"
#import "DetailsViewController.h"

@interface CompaniesTableViewController ()
@property (strong,nonatomic) NSArray *tableData;
@property(nonatomic) CGFloat estimatedRowHeight;
@end

@implementation CompaniesTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableData = @[
  @{@"name":@"1. Tata Consultancy services", @"latin":@"Revenue: 48426.14 Cr", @"details":@"Created by Ratan tata. Tata Consultancy Services (TCS) is one of the largest provider of information technology (IT) services, business solutions and consulting company headquartered in Mumbai. At present, TCS has about 2,76,000 employees across the globe.",@"other":@"Tata Consultancy Services (TCS) is one of the largest provider of information technology (IT) services, business solutions and consulting company headquartered in Mumbai. At present, TCS has about 2,76,000 employees across the globe. After the recent rally in its stock price, Tata Consultancy Services (TCS) is now the world's second-most-valuable IT services company — ahead of Accenture and Hewlett-Packard but behind IBM."},

  @{@"name":@"2. Infosys", @"latin":@"Revenue: 36765 Cr", @"details":@"Ceo is Narayan Murti",@"other":@"Founded in 1981 by Narayan Murthy, Nandan Nilekani, N. S. Raghavan, S. Gopalakrishnan, S. D. Shibulal, K. Dinesh and Ashok Arora, Infosys today is a household name and one of the biggest brands in the field of IT. It is headquartered in Bangalore, India. "},

  @{@"name":@"3. Wipro", @"latin":@"Revenue: 31682 Cr", @"details":@"Ceo is Azim Prem Ji",@"other":@"Bangalore-based Wipro is India's third largest software provider Wipro, The company has over 145,000 people across the world who are serving over 900 clients. It has presence in 53 countries and offer services such as Systems Integration, Consulting, Information Systems outsourcing, IT-enabled services, and R&D services."},

  @{@"name":@"4. HCL Technologies", @"latin":@"Revenue: 8907 Cr", @"details":@"Ceo is Anant Gupta",@"other":@"Global IT service major offers services like software-led IT solutions, remote infrastructure management, engineering and R&D services, and business process outsourcing (BPO).HCL is spread across 31 countries with about 90,000 employees across the world. Anant Gupta is currently serving as the president and CEO of HCL technologies."},

  @{@"name":@"5. Tech Mahindra", @"latin":@"Revenue: 5964 Cr", @"details":@"In 2013, Mumbai-based Mahindra Satyam was merged with Tech Mahindra. ",@"other":@"With $2.7 billion (Rs 16,000 crore) in sales, Tech Mahindra is India's fifth-largest software exporter. It employs 84,000 staff, has 540 clients, and operates in 46 countries. The company's aim is to clock $5 billion in sales by 2015. Tech Mahindra has six development centres in the US. The company's executive management team consists of Anand Mahindra, who is serving as the Chairman and Vineet Nayyar as the Vice-Chairman."},

  @{@"name":@"6. Mphasis", @"latin":@"Revenue: 3420.84 Cr", @"details":@"MphasiS is an IT service company owned by Hewlett-Packard",@"other":@"MphasiS is an IT service company owned by Hewlett-Packard. It serves various domains like BFSI, Telecom, and Logistics and provides services like infrastructure technology and applications outsourcing services, as well as architecture, application development and integration, and application management services in 19 countries like India, Sri Lanka, China, Australia, Japan, North America and Europe."},

  @{@"name":@"7. Oracle Financial Services", @"latin":@"Revenue: 2605.85 Cr", @"details":@"It has 900 clients across 145 countries.",@"other":@"Oracle Financial Services provides IT solutions to the banking sector. It has 900 clients across 145 countries. The company offers services in product division and PrimeSourcing.Especially, Oracle's FLEXCUBE caters to a vast domain of banking that includes Core Banking, Private Banking, and Lending."},

  @{@"name":@"8. MindTree", @"latin":@"Revenue: 1915 Cr", @"details":@"Owns Aadar card Project",@"other":@"Mindree is an information technology solutions company that was started in 1999 by ten industry professionals from Cambridge Technology Partners, Lucent Technologies, and Wipro.One of the most significant projects Mindtree provided to the Indian Government to provide application development, maintenance, and support services to the nation-wide Unique Identification (UID) called Aadhaar."},

  @{@"name":@"9. Polaris Tech", @"latin":@"Revenue: 1762 Cr", @"details":@"headquartered in Chennai",@"other":@"Polaris is a leading financial technology company that is headquartered in Chennai. It provides solutions mainly in Core Banking, Corporate Banking, Wealth and Asset Management and Insurance Domain."},

  @{@"name":@"10. Syntel", @"latin":@"Revenue: 1530 Cr", @"details":@"headquartered in Mumbai",@"other":@"Syntel, Inc. is a U.S.-based multinational provider of integrated technology and business services. Headquartered in Troy, Michigan, Syntel services its clients through 17 global development centers in India and the United States. Syntel is a certified minority-owned business (MBE).On April 21, 2014 Prashant Ranade, former CEO and President of Syntel was elevated to Executive Vice Chairman of the Syntel Board,while Nitin Rakesh took over as the CEO and President.Founder and Ex-CEO Bharat Desai will continue to serve as Chairman of the Board, and is the majority shareholder."},
                                            ];
    self.navigationItem.title = @"Top IT Companies Of India";

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"companyCell" forIndexPath:indexPath];

    cell.lblOutlet1.text = self.tableData[indexPath.row][@"name" ];
    cell.lblOutlet2.text = self.tableData[indexPath.row][@"latin" ];
    cell.lblOutlet3.text = self.tableData[indexPath.row][@"details" ];

    return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailsViewController *detailView = (DetailsViewController *)segue.destinationViewController;
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    NSString *dataSegue = self.tableData[selectedIndexPath.row][@"name"];
    NSString *dataSegue2 = self.tableData[selectedIndexPath.row][@"other"];
    [detailView setDataSegue:dataSegue];
    [detailView setDataSegue2:dataSegue2];

    //[detailView.navigationItem setTitle:dataSegue];
}

@end

TableViewCell.h: -

#import <UIKit/UIKit.h>
@interface TableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *lblOutlet1;

@property (strong, nonatomic) IBOutlet UILabel *lblOutlet2;

@property (strong, nonatomic) IBOutlet UILabel *lblOutlet3;

@end

2 个答案:

答案 0 :(得分:0)

将此内容写入viewDidLoad

self.tableView.estimatedRowHeight = 100 // some constant value
self.tableView.rowHeight = UITableViewAutomaticDimension

答案 1 :(得分:-1)

您必须覆盖表格单元格方法的高度: 的 1。调整单元格高度

int cellSize = 60;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

CGSize newSize = [self sizeOfText:self.tableData[indexPath.row][@"details" ] widthOfTextView:<lblOutlet3 WIDTH> withFont:<lblOutlet3 FONT>];

if(newSize.height != 0)
    return newSize.height;

return cellSize;
}

- (CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font {
if (!font) {
    font = [UIFont systemFontOfSize:18];
}
CGSize ts = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return ts;
}

您可能会忽略您收到的警告。

<强> 2。调整标签高度

现在在cellForRowAtIndexPath:

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

<Your Set of CODE>

CGSize newSize = [self sizeOfText:self.tableData[indexPath.row][@"details" ] widthOfTextView:<lblOutlet3 WIDTH> withFont:<lblOutlet3 FONT>];

cell.lblOutlet3.frame = CGRectMake(cell.lblOutlet3.frame.origin.x,cell.lblOutlet3.frame.origin.y,cell.lblOutlet3.frame.size.width,newSize.height);

return cell;
}

这会调整标签的大小。