การตั้งค่าสแตติกเซลล์ใน uitableview โดยทางโปรแกรม

ฉันกำลังสร้าง tableview โดยทางโปรแกรมในวัตถุประสงค์ c ฉันจะทำให้เซลล์คงที่โดยทางโปรแกรมได้อย่างไร

ขอบคุณ


person jpo    schedule 25.06.2012    source แหล่งที่มา


คำตอบ (5)


โดยการใช้ตัวระบุเซลล์ที่แตกต่างกันสำหรับแต่ละเซลล์ คุณจะได้รับมัน คุณสามารถใช้สิ่งนี้:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [NSString stringWithFormat:@"s%i-r%i", indexPath.section, indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        //you can customize your cell here because it will be used just for one row.
    }

    return cell;
}
person Ricard Pérez del Campo    schedule 25.06.2012

การทำให้เซลล์คงที่โดยทางโปรแกรมไม่สมเหตุสมผลเลย โดยทั่วไปเซลล์แบบคงที่มีไว้สำหรับตัวสร้างอินเทอร์เฟซเท่านั้น และต้องการให้ TableView ทั้งหมดเป็นแบบคงที่ อนุญาตให้คุณลาก UILables, UITextFields, UIImageViews ฯลฯ ลงในเซลล์และให้มันแสดงลักษณะที่ปรากฏใน Xcode เมื่อเรียกใช้แอป

อย่างไรก็ตาม เซลล์ของคุณสามารถ "คงที่" โดยทางโปรแกรมได้โดยไม่ต้องใช้แหล่งข้อมูลภายนอกและฮาร์ดโค้ดทุกอย่าง ซึ่งโดยปกติแล้วจะค่อนข้างยุ่งเหยิงและเป็นความคิดที่ไม่ดี

ฉันขอแนะนำให้สร้าง UITableViewController ใหม่ด้วย .xib และปรับแต่งจากตรงนั้นหากคุณต้องการเซลล์ "คงที่" มิฉะนั้น เพียงฮาร์ดโค้ดค่าทั้งหมดของคุณและโดยพื้นฐานแล้วมันก็เป็นสิ่งเดียวกัน แต่อาจเป็นการออกแบบที่ไม่ดีหากสามารถหลีกเลี่ยงได้

person MikeS    schedule 25.06.2012
comment
ฉันคิดว่าเป็นที่ยอมรับอย่างสมบูรณ์ในการสร้างเซลล์ tableview แบบคงที่โดยทางโปรแกรมได้มากเท่าที่คุณต้องการหากคุณต้องการหลีกเลี่ยงการใช้ InterfaceBuilder มีเหตุผลที่ถูกต้องหลายประการที่ต้องการลดการใช้ InterfacerBuilder ให้เหลือน้อยที่สุด - person MH175; 24.11.2018

คุณยังสามารถทำแบบเก่าและเพียงสร้างเซลล์ตามที่คุณต้องการขึ้นอยู่กับ NSIndexPath ซึ่งใช้ได้กับ Static Cell TVC และมุมมองตารางปกติ (อย่าลืมส่งคืนส่วนและแถวในจำนวนที่เหมาะสมในวิธีการแหล่งข้อมูลของพวกเขา ):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch indexPath.row {
        case 0:
            // First cell, setup the way you want

        case 1:
            // Second cell, setup the way you want
    }

    // return the customized cell
    return cell;
}
person LJ Wilson    schedule 25.06.2012

ฉันต้องการสร้างโครงสร้างเซลล์เช่นสำหรับหน้าจอการตั้งค่าหรืออะไรทำนองนั้นและคุณอาจต้องแก้ไขเนื้อหาเซลล์บางส่วน แต่ไม่ใช่โครงสร้างจำนวนหรือส่วน คุณสามารถโอเวอร์โหลดวิธีการของคลาสย่อย UITableViewController ของคุณดังนี้:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    // Configure the cell...
    if ([aCell.reuseIdentifier isEqualToString:@"someIdentifier"]){
        //some configuration block
    }

    else if ([aCell.reuseIdentifier isEqualToString:@"someOtherIdentifier"]) {
        //other configuration block
    }
    return aCell;
}

แต่คุณสามารถทำให้มันดีขึ้นได้โดยใช้โค้ดเพิ่มเติมอีกเล็กน้อย

1) ในตอนต้นของไฟล์ .m ของคุณ ให้เพิ่ม typedef:

typedef void(^IDPCellConfigurationBlock)(UITableViewCell *aCell);

2) เพิ่มคุณสมบัติ cellConfigurations ให้กับส่วนขยาย TablviewControllerSubclass ของคุณ:

@interface IPDSettingsTableViewController ()

@property (nonatomic, strong) NSDictionary *cellConfigurations;
@property (nonatomic) id dataModel;

@end

3) แก้ไขเซลล์สแตติกของคลาสย่อย TableviewController ในกระดานเรื่องราวหรือ xib และเพิ่ม cellReuseIdentifier ที่ไม่ซ้ำกันสำหรับแต่ละเซลล์ที่คุณต้องการแก้ไขโดยทางโปรแกรม

4) ในเซลล์การตั้งค่าเมธอด viewDidLoad ของคุณบล็อกการกำหนดค่า:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self SetupCellsConfigurationBlocks];
}

- (void)SetupCellsConfigurationBlocks
{
    //Store configurations code for each cell reuse identifier
    NSMutableDictionary *cellsConfigurationBlocks = [NSMutableDictionary new];        


    //store cells configurations for a different cells identifiers
    cellsConfigurationBlocks[@"someCellIdentifier"] = ^(UITableViewCell *aCell){
        aCell.backgroundColor = [UIColor orangeColor];
    };

    cellsConfigurationBlocks[@"otherCellIdentifier"] = ^(UITableViewCell *aCell){
        aCell.imageView.image = [UIImage imageNamed:@"some image name"];
    };

    //use waek reference to self to avoid memory leaks
    __weak typeof (self) weakSelf = self;
    cellsConfigurationBlocks[@"nextCellIdentifier"] = ^(UITableViewCell *aCell){
        //You can even use your data model to configure cell
        aCell.textLabel.textColor = [[weakSelf.dataModel someProperty] isEqual:@YES] ? [UIColor purpleColor] : [UIColor yellowColor];
        aCell.textLabel.text      = [weakSelf.dataModel someOtherProperty];
    };
    weakSelf.cellConfigurations = [cellsConfigurationBlocks copy];
}

5) วิธีการโอเวอร์โหลด tableView:cellForRowAtIndexPath เช่นนี้:

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    // configure cell
    [self configureCell:aCell withConfigurationBlock:self.cellConfigurations[aCell.reuseIdentifier]];
    return aCell;
}

- (void)configureCell:(UITableViewCell *)aCell withConfigurationBlock:(IDPCellConfigurationBlock)configureCellBlock
{
    if (configureCellBlock){
        configureCellBlock(aCell);
    }
}
person Nikolay Shubenkov    schedule 08.05.2015

เป็นเรื่องปกติที่คุณจะต้องการสร้างตารางอย่างง่ายเพื่อใช้เป็นเมนูหรือแบบฟอร์ม แต่การใช้ API ในตัวกับแหล่งข้อมูลและการเรียกกลับของผู้รับมอบสิทธิ์ไม่ได้ช่วยให้เขียนหรือบำรุงรักษาได้ง่าย คุณอาจต้องเพิ่ม/ลบ/อัปเดตบางเซลล์แบบไดนามิก ดังนั้นการใช้สตอรี่บอร์ดเพียงอย่างเดียวจะไม่ทำงาน

ฉันรวบรวม MEDeclarativeTable เพื่อสร้างตารางขนาดเล็กโดยทางโปรแกรม โดยจัดเตรียมแหล่งข้อมูลและผู้รับมอบสิทธิ์สำหรับ UITableView เราจบลงด้วย API ที่เราจัดเตรียมอินสแตนซ์ของส่วนและแถวแทนที่จะใช้แหล่งข้อมูลและวิธีการมอบหมาย

person Michael Enriquez    schedule 12.05.2015