gpt4 book ai didi

ios - 静态 UITableViewCell 背景 View

转载 作者:行者123 更新时间:2023-11-29 11:13:00 24 4
gpt4 key购买 nike

在我的项目中,我有带有静态单元格的 tableView 以及带有动态单元格的 tableView。为了进行自定义,我设法在单元格上设置了渐变背景(分组样式)。

当我根据行的位置(顶部、底部、中间或单个)在 cellForRowAtIndex 中设置背景 View 时,它可以与动态 TableView 一起使用。

但是,当我尝试在静态表格 View 单元格上实现它时,它不起作用。我已尝试实现 cellForRowAtindex...但它崩溃了。

有人有想法吗?

更新:cellForRow 的代码..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

UACellBackgroundView *bgw=[[UACellBackgroundView alloc]init];

if (indexPath.row==0) {

bgw.position = UACellBackgroundViewPositionTop;
cell.backgroundView=bgw;

}else if (indexPath.row==2){

bgw.position = UACellBackgroundViewPositionBottom;
cell.backgroundView=bgw;

}else {
bgw.position = UACellBackgroundViewPositionMiddle;
cell.backgroundView=bgw;
}

// cell.backgroundView=bgw;


return cell;
}

顺便说一下,我从这里得到的背景 View :http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds在这里:http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/

如果有人感兴趣

最佳答案

看来你不是都是UITableViewCell,需要alloc cell。

例如:

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = @"Cell Name";
cell.detailTextLabel.text = @"Cell Detail";

return cell;
}

添加此语句:

if (cell == nil) {
// alloc the UITableViewCell
// remeber if you are not using ARC you need to autorelease this cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

关于ios - 静态 UITableViewCell 背景 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668290/

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