設置我的細胞是這樣的:如何計算通過cellForRowAtIndexPath中的開關設置的單元格的heightForRowAtIndexPath。
所以,一對夫婦開關,確定細胞,因爲數據不是對象的列表,而是一組信息應在一些不同的顯示在的tableView方法。
-(UITableViewCell *)value1CellForTableView:(UITableView *)tableView {
static NSString *CellIdentifierValue1 = @"Value1Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierValue1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifierValue1];
cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textAlignment = UITextAlignmentLeft;
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
switch (indexPath.section) {
case 0:
//Coupon
switch (indexPath.row) {
case 0:
//Couponcode
cell = [self value1CellForTableView:tableView];
cell.textLabel.text = @"Code";
cell.detailTextLabel.text = presentedCoupon.couponNr;
break;
case 1:
//Coupondescription
cell = [self value1CellForTableView:tableView];
cell.detailTextLabel.text = presentedCoupon.couponDescription;
cell.detailTextLabel.numberOfLines = 0;
cell.textLabel.text [email protected]"Ihr Vorteil";
break;
}
break;
case 1:
//Productinfo
switch (indexPath.row) {
case 0:
cell = [self defaultCellForTableView:tableView];
cell.imageView.image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: [presentedCoupon.refProdName stringByAppendingString:@".png"]]];
cell.textLabel.text = presentedCoupon.refProdName;
break;
}
break;
case 2:
//Shopinfo
switch (indexPath.row) {
case 0:
cell = [self defaultCellForTableView:tableView];
cell.textLabel.text = ((Shop*)presentedCoupon.refShop).name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
break;
}
if (cell == nil) {
cell = [self defaultCellForTableView:tableView];
cell.textLabel.text = @"Stanni";
}
[cell layoutIfNeeded];
return cell;
}
而我計算這樣的高度。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"height");
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat height = 24 + [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize: CGSizeMake(cell.detailTextLabel.frame.size.width, 1000.0f) lineBreakMode:cell.detailTextLabel.lineBreakMode].height;
return MAX(height, 44.0f);
問題:
的問題是,在許多線程所提到的,在我的日誌中還可見,每個單元的高度,(可見或不可見)被要求在初始化表視圖。所以在更大的100多個列表中,還會創建100多個單元 - >在啓動時浪費。
當這樣設置單元格時,還有另一種獲取此信息的可能性嗎?是否真的需要在heightForRowAtIndexPath中再次構建開關結構結構以避免這些調用,並且爲每個單元格獲得正確的高度?
用每個單元格的單個信息來保存一個「數據源列表」會更好嗎?
但是如何處理不同的單元格樣式,自定義單元格。
1.我有非常少的細胞。也許大約10。所以你不會使用UITableView的重用事物,並將viewDidLoad中的單元格放置在額外的數組中?但是,你現在如何計算不同風格細胞的高度? 2.這就像在一個額外的方法中再次構建開關盒,因爲這些數據並不是在列表或者某物中。 有沒有比使用tableview來呈現數據更有效的方法? – Denny1989 2012-01-13 09:39:48
你的問題是你從另一個委託方法調用一個委託方法。如果您事先創建了單元格,則可以在兩個委託方法中使用它們,並且不會創建新對象。 – Sulthan 2012-01-13 10:06:41
「或測量它們的高度」 - 要做到這一點,我首先必須強制單元格佈局 - 即使它們還沒有添加到tableview中!我怎麼能這樣做? – 2013-09-09 08:45:13