我有一個UITableView
自定義單元格及其對應的.xib。 我的CustomCell.h有3個屬性。我希望能夠以編程方式指定這些屬性的寬度而不使用自動佈局。這三個屬性是標題視圖中的背景圖像,標題視圖和標題。我想將所有三個屬性的with設置爲與cell.contentView寬度相同。調整UITableView自定義單元格中的屬性
我已將[cell setNeedsLayout]
置於我的cellForRowAtIndexPath
中,但唯一更新的屬性是CustomCellOne中的ImageView
和titleView
。標籤的標籤沒有更新。除了不更新標籤寬度這個事實之外,我在這個「解決方案」中遇到的問題是,當顯示單元格時,我可以看到背景圖像寬度更新。背景圖像開始於320寬度並擴展到375寬度。我希望在顯示單元格之前執行layoutSubviews
。
我也試過把[cell setNeedsLayout]
放在willDisplayCell
裏面,但是它產生了和上面一樣的行爲。
我也試過[self setNeedsLayout]
在awakeFromNib
方法從CustomCellOne.m但也產生了上述相同的結果。
此自定義單元需要能夠考慮不同iPhone尺寸的尺寸,[ImageManager]爲特定設備返回適當的背景圖像。
如果我選擇該單元,那麼layoutSubviews
將更新,因爲我希望它們是。有沒有辦法以編程方式觸發單元格的選擇?這不是最好的解決方案,因爲它是一種黑客攻擊,我相信有更好的方式,正確的方式,做我想做的事。
我將永遠處於無法幫助我的人的債務之中。我的代碼如下。
// My ListViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.bounds = [self.view frame];
self.view.backgroundColor = [UIColor colorWithRed:0.882f green:0.902f blue:0.922f alpha:1.00f];
[self setNeedsStatusBarAppearanceUpdate];
// Setup the table view
self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
// Setup the bar
self.myCustomBar = [[StyleOneBar alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), 100.0)];
SquareCashStyleBehaviorDefiner *behaviorDefiner = [[SquareCashStyleBehaviorDefiner alloc] init];
behaviorDefiner.elasticMaximumHeightAtTop = YES;
[behaviorDefiner addSnappingPositionProgress:0.0 forProgressRangeStart:0.0 end:0.5];
[behaviorDefiner addSnappingPositionProgress:1.0 forProgressRangeStart:0.5 end:1.0];
behaviorDefiner.snappingEnabled = YES;
self.myCustomBar.behaviorDefiner = behaviorDefiner;
[self.view addSubview:self.myCustomBar];
// Configure a separate UITableViewDelegate and UIScrollViewDelegate (optional)
self.delegateSplitter = [[BLKDelegateSplitter alloc] initWithFirstDelegate:behaviorDefiner secondDelegate:self];
self.tableView.delegate = (id<UITableViewDelegate>)self.delegateSplitter;
//[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
self.tableView.contentInset = UIEdgeInsetsMake(self.myCustomBar.maximumBarHeight, 0.0, 0.0, 0.0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
CustomCellOne *cell = (CustomCellOne *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:@"CustomCellOne" bundle:nil] forCellReuseIdentifier:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
NSString *imageName = [NSString stringWithFormat:@"sunbathing%@", [ImageManager getCollectionImagePartialName]];
[cell.ivCategory setImage:[UIImage imageNamed:imageName]];
[cell.lblTitle setText:@"Lazy Beach Day"];
cell.titleView.frame = CGRectMake(0, 0, CGRectGetWidth(cell.contentView.bounds), 29);
return cell;
}
我的自定單元h文件
// CustomCellOne.h
#import <UIKit/UIKit.h>
@interface EventCellOne : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *ivCategory;
@property (retain, nonatomic) IBOutlet UIView *titleView;
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@end
我的自定單元.m文件
#import "CustomCellOne.h"
#import "ImageManager.h"
@implementation EventCellOne
- (void)awakeFromNib
{
// Initialization code
[super awakeFromNib];
self.contentView.backgroundColor = [UIColor clear];
[self setNeedsLayout];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
//self.bounds = self.contentView.bounds;
[ImageManager getCollectionImageHeight]);
CGRect imageViewBounds = CGRectMake(0, 0, [ImageManager getImageMaxWidth], [ImageManager getCollectionImageHeight]);
[self.ivCategory setFrame:imageViewBounds];
self.titleView.frame = CGRectMake(0, 0, CGRectGetWidth(self.contentView.bounds), 29);
self.titleView.backgroundColor = [UIColor redColor];
self.lblTitle.frame = CGRectMake(3, 4, CGRectGetWidth(self.contentView.bounds)-6, 21);
[self.lblTitle setBackgroundColor:[UIColor lightGrayColor]];
}
如果我錯了,請糾正我,但禁用AutoLayout時layoutSubviews不會被調用。你必須做任何繪製在直角。並確保你將autoTranslateIntoContraints設置爲false。還要看看繪圖循環如何工作。 –
@ user3654258 - 有趣的是,CustomCellOne.m中的layoutSubviews確實被調用,但該方法中沒有任何屬性框架修改生效。我改變了UIImageView,但沒有任何反應。寬度保持在該屬性的.xib中設置的任何值。 我把一個NSLog放在layoutsubviews裏面,它被調用來顯示每個單元格。 – WillJ
@ user3654258 我已經將繪圖添加到drawRect。 UIImageView和UILabel寬度確實被修改,但UILabel保持不變。無論我將其更改爲什麼寬度,它都會保留該IBOutlet的.xib中指定的寬度 – WillJ