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

时间:2014-08-07 08:58:23

标签: ios objective-c uitableview ios7

我使用以下教程创建了一个可扩展的tableview: Expandable-Collapsable-TableView

我的形象:

enter image description here

我想减少左侧每行之间的差距。距离太远了。 任何人都可以帮我定位排。

3 个答案:

答案 0 :(得分:1)

这被称为"缩进"。

如果您在storyboard / xib中使用原型单元格,则可以直接从单元格的属性检查器中更改其缩进。

enter image description here

(如果缩进级别为0,则不必将宽度更改为0)。

如果没有,您可以通过编程方式进行更改,例如:

-(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.

}

答案 1 :(得分:1)

打开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

 }

答案 2 :(得分:1)

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