【问题标题】:iOS:change the starting index of UITableView from 0 to 1 to display array contentiOS:将 UITableView 的起始索引从 0 改为 1 以显示数组内容
【发布时间】:2018-02-15 07:42:34
【问题描述】:

我在我的项目中使用 UITableView 和 2 ProtoTypeCells。我有一个主要内容的array,我想从第二个Prototypecell 即从index 1 显示。 目前我正在使用以下代码将项目隐藏在我的array 的0 index 并从array 的第一个index 开始。 有人请帮助我如何从我的 tableview 的索引 1 中显示 dataArray 的内容。

这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [DataArray count];
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int feedIndex = [indexPath indexAtPosition:[indexPath length]-1];
    NSLog(@"Feed Index -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- %d",feedIndex);
    //Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];

    static NSString *CellIdentifier1 = @"OneCellId";
    static NSString *CellIdentifier2 = @"OtherCellId";

    if(feedIndex == 0)
    {
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] ;
            [[[cell subviews] objectAtIndex:0] setTag:1];
        }
            UIImageView * imgvw = (UIImageView*)[cell viewWithTag:101];
            imgvw.layer.cornerRadius = imgvw.frame.size.width / 2;
            imgvw.clipsToBounds = YES;
            imgvw.layer.borderWidth = .5f;
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:profileImg]];
            imgvw.image = [UIImage imageWithData:imageData];

            [self hideLoadingView];
            return cell;
        //cell.feed = item;
    }
    else 
    {
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 ] ;
            [[[cell subviews] objectAtIndex:1] setTag:2];
        }

        NSString* OtherNameStr = [[DataArray objectAtIndex:indexPath.row]valueForKey:@"full_name"];
        NSLog(@"%@",OtherNameStr);
        UILabel * OtherNameLbl = (UILabel *)[cell viewWithTag:103];
        OtherNameLbl.text = OtherNameStr;

这是我在 0 索引处的屏幕截图,我正在显示其他内容,从索引 1 开始,我想从 0 索引处显示 DataArray。

【问题讨论】:

  • 只要输入条件 If(indexPath.row == 0){ return nil; } else{ 其余代码 }
  • 试过但没用
  • 您在编写[DataArray count]-1[DataArray objectAtIndex:indexPath.row+1] 时遇到问题吗?

标签: ios objective-c uitableview nsarray


【解决方案1】:

我相信在您的情况下最好的解决方案是使用部分甚至部分标题。

对于带有部分的解决方案,第一部分应显示您的“标题”,第二部分应显示您的单元格。像这样的东西应该可以工作:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0: return 1;
        case 1: return DataArray.count;
        default: return 0;
    }
}


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

    static NSString *CellIdentifier1 = @"OneCellId";
    static NSString *CellIdentifier2 = @"OtherCellId";

    switch (indexPath.section) {
        case 0: {
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; // Try having "MyTableViewCell *cell = [tableView dequ..."
            ///...
            return cell;
        }
        case 1: {
            UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; // Try having "MyOtherTableViewCell *cell = [tableView dequ..."
            ///...
            NSString* OtherNameStr = [DataArray[indexPath.row] valueForKey:@"full_name"];
            ///...
            return cell;
        }
        default: return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; // This should never happen
    }

}

编辑:(不相关)创建具体的单元格类并使用高度约束

您应该为表格视图单元格设置具体类:

标题:

@interface MyTableViewCell: UITableViewCell

@property (nonatomic, strong) UIImage *profileImage;

@end

来源:

@interface MyTableViewCell ()

@property (nonatomic, strong) IBOutlet UIImageView *profileImageView;
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *profileImageViewHeightConstraint;

@end

@implementation MyTableViewCell

- (void)setProfileImage:(UIImage *)profileImage {
    self.profileImageView.image = profileImage;
    self.profileImageViewHeightConstraint.constant = profileImage == nil ? 0.0 : 100.0;
    [self layoutIfNeeded];
}
- (UIImage *)profileImage {
    return self.profileImageView.image;
}

@end

您需要在情节提要中设置此类,并将 profileImageViewprofileImageViewHeightConstraint 两个出口与相应的元素连接。

现在你可以在你的代码中使用它:

MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

你已经暴露了所有你需要的东西,所以你可以简单地调用:

cell.profileImage = [UIImage imageWithData:imageData];

其余的应该在内部完成。如您所见,设置器被覆盖,并且约束将根据图像视图是否存在而改变。根据您的要求,如果没有传递图像,它将为 0,否则为 100。

【讨论】:

  • 你能帮我如何使用约束动态更改图像视图的高度,就好像图像可用那么高度将为 100,否则高度将为 0
【解决方案2】:

试试这个我只是改了你的代码,希望对你有帮助。

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
       return 1;
     }


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [DataArray count]+1;
}


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

    static NSString *CellIdentifier1 = @"OneCellId";
    static NSString *CellIdentifier2 = @"OtherCellId";

    if(indexPath.row == 0)
    {
         UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:CellIdentifier1 
         forIndexPath:indexPath];
[[[cell subviews] objectAtIndex:0] setTag:1];

            UIImageView * imgvw = (UIImageView*)[cell viewWithTag:101];
            imgvw.layer.cornerRadius = imgvw.frame.size.width / 2;
            imgvw.clipsToBounds = YES;
            imgvw.layer.borderWidth = .5f;
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL 
            URLWithString:profileImg]];
            imgvw.image = [UIImage imageWithData:imageData];

            [self hideLoadingView];
            return cell;
    }
    else 
    {
        UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:CellIdentifier2 
         forIndexPath:indexPath];
 [[[cell subviews] objectAtIndex:0] setTag:2];

        NSString* OtherNameStr = [[DataArray objectAtIndex:
        (indexPath.row)-1]valueForKey:@"full_name"];
        NSLog(@"%@",OtherNameStr);
        UILabel * OtherNameLbl = (UILabel *)[cell viewWithTag:103];
        OtherNameLbl.text = OtherNameStr;

        return cell;
     }
     return nil;
    }

【讨论】:

  • 你能帮我如何使用约束动态更改图像视图的高度,就好像图像可用那么高度将为 100,否则高度将为 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
相关资源
最近更新 更多