gpt4 book ai didi

ios - UITableViewCell 覆盖 : when alloc UILabel in cellForRowAtIndexPath?

转载 作者:行者123 更新时间:2023-11-29 10:40:56 25 4
gpt4 key购买 nike

我想显示 nib 文件中的 UITableviewCell。并且还想在其上添加动态标签。但是标签是自己覆盖的。请提供我可以从 nib 调用 UITableviewCell 并添加动态标签的任何方法。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil];
[[self tblView] registerNib:nib forCellReuseIdentifier:@"ItemCell"];

}

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


#pragma mark

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 150.0;
}

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

ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];

for (int i=0; i<4; i++) {
UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0*i, 150.0, 20.0)];
lbl.text=@"This is custom cell.";
[cell.contentView addSubview:lbl];
}

return cell;
}

最佳答案

使用这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath 
{
UILabel *lbl;
ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:@"ItemCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
lbl.tag = indexpath.row;
[cell.contentView addSubview:lbl];
}
lbl = (UIlabel*) [cell.contentView viewWithTag:indexpath.row];
lbl.text=@"This is custom cell.";
return cell;
}

关于ios - UITableViewCell 覆盖 : when alloc UILabel in cellForRowAtIndexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24608458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com