我正在使用 UITableView
在我的项目中有 2 ProtoTypeCells
.我有一个 array
我想从第 2 次开始显示的主要内容 Prototypecell
即来自 index
1.
目前我正在使用以下代码将项目隐藏在 0 index
我的array
从第一个index
开始的 array
.
有人请帮助我如何从我的 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。
最佳答案
我相信您的情况下最好的解决方案是使用部分甚至部分标题。
对于带有部分的解决方案,第一部分应显示您的“标题”,第二部分应显示您的单元格。这样的事情应该有效:
- (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
}
}
编辑:(不相关)创建具体单元类并使用高度限制
例如,您应该为表格 View 单元格设置具体类:
标题:
@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
您需要在您的 Storyboard中设置此类,并将导出 profileImageView
和 profileImageViewHeightConstraint
与相应的元素连接起来。
现在您可以在您的代码中使用它:
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
并且您已经公开了所有需要的内容,因此您可以简单地调用:
cell.profileImage = [UIImage imageWithData:imageData];
其余的应该在内部完成。如您所见,setter 被覆盖并且约束将根据 ImageView 是否存在而改变。正如您所要求的,如果没有传递图像,它将为 0,否则为 100。
关于iOS:将UITableView的起始索引从0改为1显示数组内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802153/