简体   繁体   中英

How to change the position of a row in UITableView in ios

I used the following tutorial to create a expandable tableview: Expandable-Collapsable-TableView

My Image:

在此处输入图片说明

I want to reduce the gap between every row from left side. The Distance is too much. Anyone can help me to position the row.

That's called "indentation".

If you're using a prototype cell in a storyboard/xib, you can directly change its indentation from the cell's Attributes Inspector.

在此处输入图片说明

(You don't have to change the width to 0, if your indentation level is 0).

If not, you can change it programmatically, like:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.indentationLevel = 0;   // No indentation level.
    cell.indentationWidth = 0;   // It's redundant if the level is 0.

}

open the createCellWithTitle method and change the cell.indentationWidth as your self

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Name"];

return [self createCellWithTitle:Title image:[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Image name"] indexPath:indexPath];
 } 



- (UITableViewCell*)createCellWithTitle:(NSString *)title image:(UIImage *)image  indexPath:(NSIndexPath*)indexPath
{
    [cell setIndentationLevel:[[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
    cell.indentationWidth = 40;   //change the width as your self

 }
- (UITableViewCell*)createCellWithTitle:(NSString *)title image:(UIImage *)image  indexPath:(NSIndexPath*)indexPath
{
    NSString *CellIdentifier = @"Cell";
    ExpandableTableViewCell* cell = [self.menuTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor grayColor];
        cell.selectedBackgroundView = bgView;
        cell.lblTitle.text = title;
        cell.lblTitle.textColor = [UIColor blackColor];

        [cell setIndentationLevel:[[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];

        //**replace your x position**
        cell.indentationWidth = 10;

        float indentPoints = cell.indentationLevel * cell.indentationWidth;

        cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);

        NSDictionary *d1=[self.itemsInTable objectAtIndex:indexPath.row] ;

        if([d1 valueForKey:@"SubItems"])
        {
            cell.btnExpand.alpha = 1.0;
            [cell.btnExpand addTarget:self action:@selector(showSubItems:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            cell.btnExpand.alpha = 0.0;
        }
        return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM