简体   繁体   English

如何在iOS中的UITableView中更改行的位置

[英]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 我使用以下教程创建了可扩展的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. 如果要在情节提要/ xib中使用原型单元,则可以直接从单元的“属性”检查器中更改其缩进。

在此处输入图片说明

(You don't have to change the width to 0, if your indentation level is 0). (如果缩进级别为0,则不必将宽度更改为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 打开createCellWithTitle方法,改变cell.indentationWidth为你的自我

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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