简体   繁体   English

iOS:如何将UITableViewCellAccessory按钮移动到左侧

[英]iOS: How to move UITableViewCellAccessory Button to the left

I'm trying to develop a UITableView which has sectionIndexTitles and which cells have accessory buttons. 我正在尝试开发一个具有sectionIndexTitles和哪些单元格具有附件按钮的UITableView。

The problem is that the scrollbar for the index titles decreases the width of the cells and so the accessory buttons are at an ugly position... 问题是索引标题的滚动条减小了单元格的宽度,因此附件按钮处于难看的位置...

Is it possible to move the button slightly to the left that they are inside the cell? 是否可以将按钮在单元格内稍微向左移动?

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"text";
    }

    return cell;
}

As you can see I don't manipulate the cell or something like that. 如您所见,我不操纵单元格或类似的东西。

And here you can see the result: 在这里您可以看到结果:

http://i.stack.imgur.com/3AB89.jpg http://i.stack.imgur.com/3AB89.jpg

There is a way to move default accessoryView, but its pretty hacky. 有一种方法可以移动默认的accessoryView,但是它很hacky。 So it might stop working one day when new sdk arrives. 因此,有一天新的SDK到达时,它可能会停止工作。

Use at your own risk (this code snippet moves any accessoryView 8 pixels to the left, insert it inside -(void)layoutSubviews method of desired uitableviewcell subclass): 使用时需要您自担风险(此代码段将所有AccessoriesView向左移动8个像素,将其插入所需uitableviewcell子类的-(void)layoutSubviews方法内):

if (self.accessoryView) {
    r = self.accessoryView.frame;
    r.origin.x -= 8;
    self.accessoryView.frame = r;
} else {
    UIView* defaultAccessoryView = nil;
    for (UIView* subview in self.subviews) {
        if (subview != self.textLabel && 
            subview != self.detailTextLabel && 
            subview != self.backgroundView && 
            subview != self.contentView &&
            subview != self.selectedBackgroundView &&
            subview != self.imageView) {
            defaultAccessoryView = subview;
            break;
        }
    }
    r = defaultAccessoryView.frame;
    r.origin.x -= 8;
    defaultAccessoryView.frame = r;
}

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

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