0
我有9節和56行的表格。重用小區... :(
我要添加一個文本標籤爲每個小區。我創建了一個NSArray
和含有該號碼的數組。在每一節(sectionsArray
)行
這裏是我的代碼,但它不能正常工作在所有?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Use existing cell (reusable)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
//If no existing cell, create a new one
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
//Define cell accessory type
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//Create a subView for the cell
CGRect subViewFrame = cell.contentView.frame;
subViewFrame.origin.x += kInset;
subViewFrame.size.width = kInset + kSelectLabelWidth;
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame];
//SubView design
selectedLabel.textColor = [UIColor blackColor];
selectedLabel.highlightedTextColor = [UIColor whiteColor];
selectedLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:selectedLabel];
int indRow = 0;
for (int i =0; i < indexPath.section; i++) {
indRow += [[sectionsArray objectAtIndex:i] intValue];
}
indRow += indexPath.row;
NSDictionary *cellText = [menuList objectAtIndex:indRow];
selectedLabel.text = [cellText objectForKey:@"selection"];
[selectedLabel release];
}
return cell;
}
什麼問題在此代碼
在iOSSimulator
中,我看到單元格的文本有時在滾動時發生更改,標籤的順序也不正確。
我是iOS開發新手,我真的沒有得到我必須做的。我必須改變一些事情嗎? 「if(cell == nil)」??謝謝。 – MosHa 2011-05-15 09:34:45
我已經用更多的信息更新了答案。希望現在對你有意義。 – idz 2011-05-15 09:53:39
不!當我滾動表格時,應用程序正在終止... – MosHa 2011-05-15 10:02:14