我一直在玩弄定制单元在一个UITableViewController由具有基本单元(BaseCell - 的UITableViewCell的子类),然后BaseCell的子类(Sub1Cell,Sub2Cell,BaseCell的两个子类).
因为子类共享一些相同的功能,如果我完全设置它们
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
我开始为每种细胞类型重复代码.将通用设置代码放在实际的自定义UITableViewCell类中是否有一个好地方并且是一种好的做法?目前我写了一个简单的设置方法:
- (void)setupCell
{
self.picture.layer.cornerRadius = 5.0f;
self.picture.layer.masksToBounds = YES;
self.picture.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.picture.layer.borderWidth = 1.0f;
}
Run Code Online (Sandbox Code Playgroud)
一旦我创建了我的单元格,我就一直在调用它:
Sub1Cell *cell = [tableView dequeueReusableCellWithIdentifier:statusCellIdentifier];
[cell setupCell];
Run Code Online (Sandbox Code Playgroud)
有没有可以自动调用的方法?我试过 - (void)prepareForReuse但显然,它并不是每次调用,只有在重用单元格时才会调用它.
有关最佳方法的任何建议吗?
编辑:
似乎每次打电话时都会被调用tableView:cellForRowAtIndexPath.我对创建自定义单元格的正确方法感到困惑.我应该做的事情是:
Sub1Cell *cell = [tableview dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[Sub1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell setupCell];
}
Run Code Online (Sandbox Code Playgroud)
如果我没有对传递的样式做任何事情,它会影响我的自定义单元格吗?
我正在尝试读取文件的前四个字节.我知道这可以正常使用以下C代码:
FILE *file = fopen(URL.path.UTF8String, "rb");
uint data;
fread(&data, 4, 1, file);
NSLog(@"%u", data);
Run Code Online (Sandbox Code Playgroud)
打印出来:205
我试图在Objective-C/Cocoa函数中找到相同的方法.我尝试过很多东西.我觉得以下是接近的:
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:URL error:nil];
NSData *data2 = [fileHandle readDataOfLength:4];
NSLog(@"%@", data2);
NSLog(@"%u", (uint)data2.bytes);
Run Code Online (Sandbox Code Playgroud)
这打印出:<cd000000>
并且:1703552
正如所料,文件的前四个字节确实是CD000000.
我假设有两个因素导致差异(或两者):
fread不计算CD之后的0.我已经通过fileHandle读取1个字节来证实了这一点,但有时这个数字会扩展到大于一个字节,因此我不能像这样限制它.我是否需要手动检查进来的字节不是00?
这与字节序有关.我已经尝试了许多功能,如CFSwapInt32BigToHost,但无法恢复正确的值.如果任何人都能告诉我关于字节序如何工作/影响这将是很好的.