i am using UITableView
in my project with 2 ProtoTypeCells
. I have an array
of main content which i want to display from 2nd Prototypecell
i.e. from index
1.
currently i am using the following code which hide the item at 0 index
of my array
and start from first index
of array
.
someone please help me how i show the content of dataArray from the index 1 of my tableview.
Here is my code:
- (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;
here is my screenshot at 0 index i am showing other thing and from index 1 i want to display DataArray from 0 index.
I believe the best solution in your case is using sections or even section headers.
For solution with sections the first section should show your "header" and the second section your cells. Something like this should work:
- (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
}
}
EDIT: (unrelated) Creating concrete cell class and using height constraints
You should be having concrete class for instance for your table view cell:
Header:
@interface MyTableViewCell: UITableViewCell
@property (nonatomic, strong) UIImage *profileImage;
@end
Source:
@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
You need to set this class in your storyboard and connect both outlets profileImageView
and profileImageViewHeightConstraint
with corresponding elements.
Now you can use it in your code as:
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
And you have all you need exposed so you can simply call:
cell.profileImage = [UIImage imageWithData:imageData];
The rest should be accomplished internally. As you can see the setter is overridden and constraint will change depending if image view exists or not. As you requested, it will have 0 if no image is passed, otherwise 100.