Iphone 在UITableView中硬编码部分的正确方式是什么?
我有一个UITableView,其中有3个部分是硬编码的。一切正常,但我不确定我是否做得对 定义节中的行数:Iphone 在UITableView中硬编码部分的正确方式是什么?,iphone,objective-c,cocoa-touch,xcode,uitableview,Iphone,Objective C,Cocoa Touch,Xcode,Uitableview,我有一个UITableView,其中有3个部分是硬编码的。一切正常,但我不确定我是否做得对 定义节中的行数: - (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section { NSInteger rows; //Bio Section if(section == 0){ rows = 2; }
- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section
{
NSInteger rows;
//Bio Section
if(section == 0){
rows = 2;
}
//Profile section
else if(section == 1){
rows = 5;
}
//Count section
else if(section == 2){
rows = 3;
}
}
return rows;
}
这里是我构建细胞的地方:
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.numberOfLines = 5;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:(10.0)];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
if ([self.message_source isEqualToString:@"default"]) {
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = [Utils formatMessage:[NSString stringWithFormat: @"%@", mySTUser.bio]];
cell.detailTextLabel.text = nil;
}
else if(indexPath.row == 1){
cell.textLabel.text = [NSString stringWithFormat: @"%@", mySTUser.website];
cell.detailTextLabel.text = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
} //more code exists, but you get the point...
现在我定义了我的节数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tblView
{
return 3;
}
这是硬编码UITableView的正确方法吗?当单元被重用时,我会遇到任何问题吗?< P>您可以考虑使用<代码>转换case < /C>树,用AN替换“<代码>如果条件,对各种硬编码整数进行测试。更详细地解释此选项。在表视图委托方法中使用
switch case
,将使代码更具可读性和灵活性。否则,您的重用代码看起来是正确的。 您可以考虑使用<代码>转换case < /COD>树,用AN替换“<代码>如果条件,对各种硬编码整数进行测试。更详细地解释此选项。在表视图委托方法中使用switch case
,将使代码更具可读性和灵活性。否则,您的重用代码看起来是正确的。要干净得多。非常感谢。干净多了。非常感谢。