gpt4 book ai didi

iphone - 更改部分的 uiTableview 单元格样式

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:19 24 4
gpt4 key购买 nike

关于更改单元格样式的问题。在 Storyboard中,我创建了一个单元格:content: dynamic prototypes

在代码中,节数:2.

第一部分可以使用 Storyboard中创建的布局。单元格中充满了来自数据库的数据。我正在尝试将第 2 部分中单元格的布局更改为 value1 样式。

如何做到这一点?

部分代码:

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

switch (indexPath.section) {

case 0:
{
// Configure the cell...
NSInteger row = [indexPath row];
[[cell textLabel] setText:[cartItems objectAtIndex:row]];

return cell;
break;
}

case 1:
{


NSInteger row = [indexPath row];
switch (row) {
case 0:
[[cell textLabel] setText:@"Total1"];
break;
case 1:
[[cell textLabel] setText:@"Total2"];
}

return cell;
break;
}
}
}

最佳答案

这样做的方法是在 Storyboard中创建 2 个不同的动态原型(prototype),每个原型(prototype)都有自己的标识符。然后在 cellForRowAtIndexPath 中,只需在 switch 语句或 else-if 子句中将具有您想要的标识符的单元格出列:

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

if (indexPath.section == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.section][indexPath.row];
return cell;

}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell_value1" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.section][indexPath.row];
cell.detailTextLabel.text = @"detail";
return cell;

}
}

关于iphone - 更改部分的 uiTableview 单元格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926545/

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