关于改变细胞样式的问题。 在故事板中,我创建了一个单元格:content:dynamic prototypes
代码中的,部分数量:2。
第一部分可以使用故事板中创建的布局。这些单元格正在填充来自数据库的数据。 我正在尝试将第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;
}
}
}
答案 0 :(得分:2)
这样做的方法是在故事板中创建2个不同的动态原型,每个原型都有自己的标识符。然后在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;
}
}
答案 1 :(得分:0)
您需要为单元格设置单元格样式 请尝试下面的代码
There are four inbuilt styles supported in iOS
1. UITableViewCellStyleDefault
2. UITableViewCellStyleValue1
3. UITableViewCellStyleValue2
4. UITableViewCellStyleSubtitle
尝试使用它们并获得所需的结果
static NSString * reuseIdentifer = @“ReuseIdentifier”;
// Try to reuse the cell from table view
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
// If there are no reusable cells; create new one
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reuseIdentifer];
}
cell.textLabel.tex = @"Your desired text";
return cell;