我在项目中使用UITableView
并{2} ProtoTypeCells
。我有一个array
主要内容,我希望从第二个Prototypecell
显示,即index
1。
目前我正在使用以下代码隐藏该项目index
的{{1}},并从array
的{{1}}开始。
有人请帮助我如何从我的tableview索引1中显示dataArray的内容。
这是我的代码:
index
这是我在0索引处的截图我正在显示其他东西,从索引1我想从0索引显示DataArray。
答案 0 :(得分: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
您需要在故事板中设置此类,并将两个出口profileImageView
和profileImageViewHeightConstraint
与相应的元素相连接。
现在您可以在代码中使用它:
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
你需要暴露所有这些,所以你可以直接打电话:
cell.profileImage = [UIImage imageWithData:imageData];
其余的应该在内部完成。正如您所看到的那样,重写setter并且约束将根据图像视图是否存在而改变。根据您的要求,如果没有传递图像,它将为0,否则为100。
答案 1 :(得分:1)
尝试这个我只需更改您的代码,希望它能为您提供帮助。
- (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;
}