How do you create a custom prototype cell with mas

2019-04-02 03:54发布

问题:

I created an xcode project with the master detail template. I want to customize the cell to put my data into 2 separate labels. I tried customizing the cell in the storyboard editor, but it seems like all of the stuff on the cell is locked in place. I am fine either creating the custom cell programmatically or in the storyboard editor. I know I could just set the text on the default textLabel to blank and then create the other labels programmatically, but that doesn't seem very need to just have a random empty textLabel in the middle of every cell. So I am wondering if there is a way to edit the cell in storyboard editor, delete the default textLabel, or resize and reposition the textLabel to where I need it to go.

Any help is greatly appreciated.

Thanks in advance.

回答1:

That's standard practice. Just ignore the built in textLabel and be done with it. By default the label is blank anyway. It's set in code.

To access new labels and such, create a new custom class for the custom cell (File|New|File...|Objective-C Class in XCode), give it a name and for the subclass choose UITableViewCell. Finally, on the Identity Inspector for the custom cell in Interface Builder, choose that new class for the Custom Class.

Now, from the Assistant Editor in XCode, you can CTRL-drag new labels and such to the .h file of the new custom cell class to create IBOutlet properties.

Be sure to set the cell Identifier on the Attributes Inspector tab for the cell and reference that in your code, especially in cellForRowAtIndexPath.

Import the custom cell class's .h file in the view controller's .h file, then cast the cell to your custom cell class in cellForRowAtIndexPath to access the new properties or change the definition UITableViewCell *cell = to your new class like MyTableViewCell *cell =.



回答2:

By default, the Style of the table cell is "Basic" in Master-Detail projects. Select the Table View Cell and open the Attributes Inspector. Change the Style from Basic to Custom.

Doing this removes the default label and allows you to add new controls.