-(UITableViewCell*)customCellWithOutXib:(UITableView *)tableView withIndexPath:(NSIndexPath*)indexPath{
//定義標識符
static NSString*customCellIndentifier = @"CustomCellIndentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIndentifier];
//定義新的cell
if(cell == nil){
//使用默認的UITableViewCell,但是不使用默認的image與text,改爲添加自定義的控件
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:customCellIndentifier];
//姓名
CGRect nameRect = CGRectMake(88, 15, 70,25); UILabel *nameLabel =[[UILabel alloc]initWithFrame:nameRect]; nameLabel.font = [UIFont boldSystemFontOfSize:nameFontSize]; nameLabel.tag = nameTag;//設置tag,以便後面的定位
nameLabel.textColor = [UIColorbrownColor]; [cell.contentViewaddSubview:nameLabel]; //班級
CGRect classTipRect = CGRectMake(88, 40, 40, 14); UILabel *classTipLabel = [[UILabelalloc]initWithFrame:classTipRect]; classTipLabel.text = @"班級:"; classTipLabel.font = [UIFontboldSystemFontOfSize:fontSize]; [cell.contentView addSubview:classTipLabel]; CGRect classRect =CGRectMake(135, 40, 40, 14); UILabel *classLabel = [[UILabel alloc]initWithFrame:classRect]; classLabel.tag = classTag;
classLabel.font = [UIFontboldSystemFontOfSize:fontSize]; [cell.contentView addSubview:classLabel];
//學號
CGRect stuNameTipRect = CGRectMake(88, 60,40, 12); UILabel *stuNameTipLabel= [[UILabel alloc]initWithFrame:stuNameTipRect]; stuNameTipLabel.text = @"學號:"; stuNameTipLabel.font = [UIFontboldSystemFontOfSize:fontSize]; [cell.contentView addSubview:stuNameTipLabel]; CGRect stuNameRect =CGRectMake(135, 60, 150, 14); UILabel *stuNameLabel = [[UILabel alloc]initWithFrame:stuNameRect]; stuNameLabel.tag = stuNumberTag; stuNameLabel.font = [UIFontboldSystemFontOfSize:fontSize]; [cell.contentView addSubview:stuNameLabel];
//圖片
CGRect imageRect = CGRectMake(15, 15, 60,60); UIImageView *imageView =[[UIImageView alloc]initWithFrame:imageRect]; imageView.tag = imageTag;
//爲圖片添加邊框
CALayer *layer = [imageView layer]; layer.cornerRadius = 8;//角的弧度 layer.borderColor = [[UIColorwhiteColor]CGColor]; layer.borderWidth = 1;//邊框寬度 layer.masksToBounds = YES;//圖片填充邊框 [cell.contentView addSubview:imageView];
}
//獲得行數
NSUInteger row = [indexPath row];
//取得相應行數的數據(NSDictionary類型,包括姓名、班級、學號、圖片名稱)
NSDictionary *dic = [_stuArrayobjectAtIndex:row]; //設置圖片
UIImageView *imageV = (UIImageView *) [cell.contentViewviewWithTag:imageTag]; imageV.image =[UIImage imageNamed:[dic objectForKey:@"image"]];
//設置姓名
UILabel *name = (UILabel*)[cell.contentView viewWithTag:nameTag];
name.text = [dicobjectForKey:@"name"];
//設置班級
UILabel *class = (UILabel*)[cell.contentView viewWithTag:classTag];
class.text = [dicobjectForKey:@"class"];
//設置學號
UILabel *stuNumber = (UILabel*)[cell.contentView viewWithTag:stuNumberTag];
stuNumber.text = [dicobjectForKey:@"stuNumber"]; //設置右側箭頭
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator; return cell;
}