[英]iOS - Constraints - Two Views next to each other in UITableViewCell
[英]Show alternate views in each UITableViewCell - iOS
我在UITableViewCell
有2個視圖,我想在每個單元格中顯示備用視圖(如果前一個單元格有正確的視圖為ON,則下一個單元格將使視圖保持打開狀態)。
我已經成功實現了它,但在滾動tableview時,交替的視圖無法正常工作; 示例圖片如下:
我的代碼是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveStreamCell" forIndexPath:indexPath];
if(cell == nil)
{
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LiveStreamCell"];
}
UIView *leftView = (UIView*)[cell viewWithTag:999];
UIView *rightView = (UIView*)[cell viewWithTag:998];
UIImageView *mediaImage;
mediaImage.clipsToBounds = YES;
UILabel *artistLabel;
UILabel *titleLabel;
UILabel *statusLabel;
if (isViewOnLeft) {
isViewOnLeft = NO;
rightView.hidden = NO;
leftView.hidden = YES;
mediaImage = (UIImageView *)[cell viewWithTag:221];
CALayer *cellImageLayer = mediaImage.layer;
[cellImageLayer setCornerRadius:74];
[cellImageLayer setMasksToBounds:YES];
artistLabel = (UILabel *)[cell.contentView viewWithTag:222];
titleLabel = (UILabel *)[cell.contentView viewWithTag:223];
statusLabel = (UILabel *)[cell.contentView viewWithTag:224];
}
else {
isViewOnLeft = YES;
leftView.hidden = NO;
rightView.hidden = YES;
mediaImage = (UIImageView *)[cell viewWithTag:121];
CALayer *cellImageLayer = mediaImage.layer;
[cellImageLayer setCornerRadius:74];
[cellImageLayer setMasksToBounds:YES];
artistLabel = (UILabel *)[cell.contentView viewWithTag:122];
titleLabel = (UILabel *)[cell.contentView viewWithTag:123];
statusLabel = (UILabel *)[cell.contentView viewWithTag:124];
}
/*Here I'm setting image (come from web URLs) and labels data*/
}
故事板截圖:
我知道問題在於細胞的可重用性,但我搜索了很多但沒有運氣。 請提出解決方案。
謝謝!
您可以創建兩個表視圖單元格,並基於indexPath,如果奇數或偶數加載左或右單元格。 假設您有奇數加載左圖像視圖。
這樣你就必須改變獲取單元格的代碼。 其余部分保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.