<分区>
标签 iphone objective-c ios grid
<分区>
我正在开发 iPhone 应用程序。我想要一个由看不见的小方 block 组成的网格。
我希望能够通过代码更改这些方 block 的颜色。
我该怎么做?
附加信息:
网格是静态的(某种国际象棋网格),我需要能够更改其中给定方 block 的颜色。例如将方 block C3 的颜色更改为红色,将 E7 的颜色更改为绿色。
除了给方 block 着色之外,我不想在方 block 中放置任何内容。
最佳答案
您可以子类化 UIView,添加一些数据结构来保存单元格信息并覆盖 drawRect:方法。
类似这样的事情:
// in view
-(void)drawCellAtRow:(int)row column:(int)column
{
// draw a cell
}
-(void)drawRect:(CGRect)rect
{
// determine which cells have to be drawn for this rect
// and draw them
}
-(void)changeCellAtRow:(int)row column:(int)column
{
// change cell info
// calculate rect to update
[self setNeedsDisplayInRect:dirtyRect];
}
// in view controller
-(void)eventHandler
{
[cellView changeCellAtRow:row column:column];
}
关于iphone - 以编程方式为网格中的单元格着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13046542/