gpt4 book ai didi

ios - 如何为每个 UITableView 部分设置不同的 Bool 值

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

我在这里要做的是设置它,以便如果 top3ArrayForSection.count-1 < 1 , 设置 _results 相应部分 的 bool 值到 NO,依此类推。相反,发生了什么事 _results对于整个表整体设​​置为 NO 或 YES,所以我最终得到这样的结果:

enter image description here

当只有“xperia Z3 compact unlocked”部分应该显示“找不到任何项目等”时。因为它没有单元格,所以其他部分的单元格不应该。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

if (top3ArrayForSection.count-1 < 1){
_results = NO;
_rowCount = 1;
}
else if(top3ArrayForSection.count-1 >= 1){
_results = YES;
_rowCount = top3ArrayForSection.count-1;
}

return _rowCount;
}


// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];

if (_results == NO) {

// title of the item
cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

}

else if (_results == YES) {

// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];

}

return cell;

}

最佳答案

您的实例变量 _results 是一维的。您可以将其替换为 NSArray 并单独存储值,或者您可以这样更改代码中的逻辑:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *currentSectionDictionary = _matchCenterArray[section];
NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

return (top3ArrayForSection.count-1 < 1) ? 1 : top3ArrayForSection.count-1;
}


// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];

NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

if (top3ArrayForSection.count-1 < 1) {

// title of the item
cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

}

else {

// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];

}

return cell;

}

关于ios - 如何为每个 UITableView 部分设置不同的 Bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26389453/

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